Nohup Command Examples – Runs a Command that Keeps Running after You Log Out

The nohup utility executes a command line such that the command keeps running after you log out. In other words, nohup causes a process to ignore a SIGHUP signal. Depending on how the local shell is configured, a process started without nohup and running in the background might be killed when you log out.

Syntax for running nohup command

The basic command sytax for running a nohup command is as follows:

# nohup command_with_options &

Any output from your job will be redirected to a file called ‘ nohup.out ’, unless you specify otherwise with

# nohup command > myout.file 2>&1

which sends all output to the file myout.file.

Saving the output of nohup command

If you do not redirect the output from a command you execute using nohup, both standard output and standard error are sent to the file named nohup.out in the working directory. If you do not have write permission for the working directory, nohup sends output to ~/nohup.out.

You can also send the output to any other file specified by you as shown above in the sytax of the command.

Examples

1. Running nohup without specifying an output file

Let’s do an example and test that it actually does continue working after you have logged out. We’ll kick off a script called script.sh. The contect of the script is as follows:

# cat script.sh
#!/bin/sh
while(true)
do
 echo "Hello World!"
 sleep 10
done

Lets run the script in backgroupd with nohup command:

# nohup script.sh & 
[1] 179 
$ nohup: appending output to `nohup.out`

As you can see above we have not specified an output file. Thus the standard output and standard error by default goes into the nohup.out file in the directory from where the script has been invoked. Also the number 179 Specified the process ID(PID) of the job. This PID can be verified in the “ps -ef” command output as shown below.

Now exit the shell, log back in again, and run the following command:

$ ps x | grep script.sh
179  ?   S N  0:01 sh /root/script.sh 
506  p2 S     0:00 grep script.sh

The script is still working. Use “ps -ef | grep ps1” if your system does not support ps x. You can also check the output file nohup.out for more confirmation.

# cat nohup.out
Hello World!
Hello World!
Hello World!
Hello World!
...
NOTE: If you run another command using nohup, the output is appended to the existing nohup.out file. Be careful when running multiple commands from the same directory, because all the output is sent to the same nohup.out file, which can get confusing.

Running nohup specifying an output file

Now the above script can also be run nohup command and specifying a custom output file as shown below:

# nohup script.sh > /tmp/script.out 2>&1
[1] 14235

Conclusion

If you are running a process and you don’t think it will be completed by the time you log out for the day, use the nohup command. Nohup will continue processing when you exit your account. Nohup means no hang up.

Related Post