DEV Community

Kyle Chilcutt
Kyle Chilcutt

Posted on • Originally published at blog.chilcutt.com on

nohup for running remote scripts

I’ve been aware of nohup for a while, but never took the time to do a deeper dive until today.

tl;dr run a command that won’t die when you log out (useful for tasks on a remote server)

$ nohup my_long_running_script > script_results.out 2>&1 &

The nohup utility lets your run another command that is immune to hangups, meaning that it won’t die when you log out of the machine. This is particularly useful for when you’re connected to the terminal of a remote machine (e.g. using ssh).

Let’s break down the example from the beginning:

$ nohup my_long_running_script > script_results.out 2>&1 &

The nohup utility causes the script being run to be immune to SIGHUP, the signal which is sent to a process when the terminal controlling it is closed.

$ nohup my_long_running_script > script_results.out 2>&1 &

The next part is running the command as usual and sending the output to a text file. Because we’re going to disconnect from this terminal session, we’re not going to be observing the output live as the script runs. To capture the output, we direct the output to a file script_results.out.

$ nohup my_long_running_script > script_results.out 2>&1 &

2>&1 redirects STDERR to STDOUT, which will allow us to capture both normal output and error output in our results file.

$ nohup my_long_running_script > script_results.out 2>&1 &

The final & causes the nohup command to be run in the background, so that we’re returned immediately to the shell once the command has been started.


Prior to this, I might have started a tmux or screen session on the remote machine to make sure the script cannot be interrupted, but nohup feels a lot more straight-to-the-point.

Top comments (0)