Nohup - Run a Command Even Once your Shell is Closed

  • July 8, 2008
  • Avatar for peter
    Peter
    Upfold

Remote server - source http://www.sxc.hu/photo/869240

Oftentimes you'll be in a situation where you want to run a command on a remote machine that will take a long time to complete, but you want to be able to issue the command and then log off and have that command run in the background.

There are many ways you could achieve this, perhaps by using cron or at to schedule the command to run right away. However, there is a better way.

There is a command called nohup built into both the GNU toolset, and most shells, which allows you to run a command in this way. It is so called because the command being run is executed ignoring 'hang up' signals, which are given when you close the terminal you started the program from.

To use this, simply prefix your command with nohup, for example:

nohup wget bigfile

This will still run in the foreground, however, meaning that you will lose the ability to use that terminal while the command is executing. In most cases, you'll want to use the ampersand (&) to run the command in the background.

nohup wget bigfile &

Now you can log off your remote machine, or close your terminal and the command will continue running in the background.

The output and errors from the command you run with nohup are stored in a file called nohup.out in the directory where you started the command, or your home directory if for some reason that's not possible (e.g. permissions).

[image source]

Avatar for peter Peter Upfold

Home » Articles »