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.
- The task assigned is completed
- You kill the process explicitly
- 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 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]
nohup will run in the foreground, and the output of the command will be stored in the
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'
cat the contents of the output file, we see this.
root@kedar:/# cat nohup.out 'My name is Kedar'
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.
Well, we can also run this command background. To run it in the background, we use the
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 &  24 root@kedar:/# nohup: ignoring input and appending output to 'nohup.out'
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
if you enjoyed it, don't forget to ❤ or 🔖.