DEV Community

Cover image for What is nohup in Linux?
Kedar Kodgire
Kedar Kodgire

Posted on

What is nohup in Linux?

When we execute a command in Linux, it creates our process in the background and assigns a unique process id. This process can end in three ways.

  1. The task assigned is completed
  2. You kill the process explicitly
  3. You log off, or in the case of SSH, the connection drops, and the session is terminated

Imagine you are running a critical process, and you might need to log off urgently or the connection drops, the process stops immediately, and you might lose your work.

To avoid this, we can use the nohup command. nohup in Linux executes other commands specified in the arguments. This command ignores all SIGHUP (hangup) signals. SIGHUP is sent to a process when its controlling terminal is closed.

To understand better, let's see the syntax.

nohup COMMAND [ARGS]

Running command in the foreground

The nohup will run in the foreground, and the output of the command will be stored in the nohup.out file. nohup command will create this file in the current directory. If the user doesn't have sufficient permissions to write a file in the current directory, nohup will generate the output file in the home directory.

Let's look at example output.

root@kedar:/# MYCOMMAND="echo 'My name is Kedar.'"

root@kedar:/# nohup $MYCOMMAND
nohup: ignoring input and appending output to 'nohup.out'
Enter fullscreen mode Exit fullscreen mode

When we cat the contents of the output file, we see this.

root@kedar:/# cat nohup.out 
'My name is Kedar'
Enter fullscreen mode Exit fullscreen mode

We have used a small command, so it executes immediately. For an extensive process, you can log out at this point, and the process will still run.

Running command in the background.

Well, we can also run this command background. To run it in the background, we use the & operator.

If we run the same command in the background, we get the following output.

root@kedar:/# MYCOMMAND="echo 'My name is Kedar.'"
root@kedar:/# nohup $MYCOMMAND &
[1] 24
root@kedar:/# nohup: ignoring input and appending output to 'nohup.out'
Enter fullscreen mode Exit fullscreen mode

The integer you see in the output is a process id.

You can also kill this background process using process id.

kill -9 24


That's all for this post, guys. Hopefully, now you have an idea about the nohup command.

if you enjoyed it, don't forget to ❤ or 🔖.

Happy learning.

Top comments (3)

Collapse
 
ianwijma profile image
Ian Wijma

Personally if I connect to a server and I'm going to do some sensitive work. I often use screen to ensure my session persist. This also allows you to continue your work on connection lost.

Collapse
 
dgihost profile image
Gurpinder Singh

Using screen or similar terminal multiplexer tools like tmux is a great practice for maintaining persistent sessions, especially when working on a remote server or conducting sensitive work. These tools allow you to create multiple virtual terminal instances within a single session, providing the following benefits:
Session Persistence: If your connection is lost or if you log out, your processes continue running within the screen or tmux session. Upon reconnecting to the server, you can resume your work right where you left off.

Multiple Windows/Tabs: screen or tmux enable you to create and manage multiple windows or tabs within the same terminal session. This helps organize different tasks or processes without cluttering the workspace.

Detach and Reattach: You can detach from a screen or tmux session without terminating the processes running within it. Later, you can reattach to the same session from another location or after reconnecting to the server.

Here are some basic commands for screen:
**

screen
Enter fullscreen mode Exit fullscreen mode

**

(To create a new session or resume an existing one.)
Detach from a screen session:

Press Ctrl + A, then D

Enter fullscreen mode Exit fullscreen mode

(This detaches you from the session without terminating it.)

List existing screen sessions:

screen -ls

Enter fullscreen mode Exit fullscreen mode

(This shows all available screen sessions.)

Reattach to a screen session:

screen -r [session_id]

Enter fullscreen mode Exit fullscreen mode

(This reattaches you to a specific screen session.)

For tmux, similar concepts apply with different commands. For example:

Starting a tmux session:

tmux

Enter fullscreen mode Exit fullscreen mode

Detach from a tmux session:

Press Ctrl + B, then D

Enter fullscreen mode Exit fullscreen mode

List existing tmux sessions:

tmux ls

Enter fullscreen mode Exit fullscreen mode

Reattach to a tmux session:

tmux attach-session -t [session_id]

Enter fullscreen mode Exit fullscreen mode

Remember, while these tools provide session persistence, it's still essential to follow proper security practices. Ensure strong authentication methods, regularly update your software, and maintain secure access controls to protect sensitive data or operations.

Collapse
 
kedark profile image
Kedar Kodgire

This seems interesting!! Thanks for sharing alternative.