Low Orbit Flux Logo 2 D

Linux - How to Nohup

The nohup command is the no hangup command. It prevents a process from being terminated when the shell that launched it is killed. An example scenario where you might use this is if you launch a command on a remote server and want it to continue running after you close your terminal and reboot your laptop.

You can run nohup on Linux like this:

nohup ./test.sh &

The nohup command will cause the process not to be terminated and the ampersand will place it in the background. It will write to the file nohup.out in the same directory. If you open another terminal you will be able to watch the output of the program by watching this file. This works great if you want to come back later and check the status of the process. For example, you could tail the nohup file like this:

tail -f nohup.out

You could verify that the process is running like this:

ps -ef |grep -i test

If you want to force it to stop you can just use the kill command like this:

kill 55972

If that doesn’t work you can always use kill -9 like this:

kill -9 55972

If you haven’t killed the original terminal yet, you can use the fg command to bring it back to the foreground ( it will still write to nohup ). You can also specify the job ID to bring to the foreground if you have more than one in the background. You can list running jobs with the jobs command.

jobs   # list jobs
fg     # bring job to foreground
fg 1   # bring job with ID 1 ( not PID ) to foreground

If you run a job like this without the ampersand:

nohup ./test.sh

You can first move it to a stopped state by pressing [ctrl] - z. Then use the bg command to move it to the background ( running ).

jobs     # find job ID 
bg 1     # specify ID
bg       # don't need ID if only one job

Redirecting stdout and stderr

You can choose to direct stdout and stderr to a file of your choosing like this:

nohup ./test.sh > out.log 2>&1 &

nohup vs disown vs &