Nohup - Run a Command Even Once your Shell is Closed

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 - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Nohup - Run a Command Even Once your Shell is Closed

  1. Scott (guest)

    # Posted on 08 July 2008 at 07:12 PM

    Great tip! Thanks.



  2. Nick (guest)

    # Posted on 09 July 2008 at 06:38 AM

    You can also use screen to accomplish the same thing, but you'll be able to return to the controlling terminal after you close the session. It's great for things like IRC. Check it out: http://www.gnu.org/software/screen/



  3. Saeid (guest)

    # Posted on 09 July 2008 at 07:28 AM

    Thanks



  4. Uddit (guest)

    # Posted on 09 July 2008 at 08:52 PM

    +1 for screen. it''s infintely useful.



  5. pvandewyngaerde (guest)

    # Posted on 10 July 2008 at 08:49 AM

    for a process you allreay started you can suspend first with ctrl+ z and then disown



  6. # Posted on 10 July 2008 at 05:41 PM

    [...] recently posted about using nohup to run a command, particularly on a remote machine, that keeps running even when you close the [...]



  7. Chape (guest)

    # Posted on 15 July 2008 at 02:13 PM

    Great tip, thanks!

    pvandewyngaerde - I understand you can put a process in the background with crtl+z but you won't be able to logout while it's still running in the background. What do you mean by disowning it?



  8. Ashish Shukla (guest)

    # Posted on 28 July 2008 at 11:23 AM

    [abbe@chateau ~]$ help disown disown: disown [-h] [-ar] [jobspec ...] By default, removes each JOBSPEC argument from the table of active jobs. If the -h option is given, the job is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. The -a option, when JOBSPEC is not supplied, means to remove all jobs from the job table; the -r option means to remove only running jobs.



  9. Cory Lievers (guest)

    # Posted on 28 July 2008 at 02:35 PM

    +1 for Screen as well, it works great. But, I actually just learned about this nohup the other day. I'm currently setting up a raid (mdadm) and articles I've read on the raid monitoring - prefixed with nohup, then it runs in the background and sends an email if there are problems. Thanks



  10. Scott (guest)

    # Posted on 02 March 2009 at 09:35 PM

    Thanks. Just what I was looking for.



  11. tymnThony (guest)

    # Posted on 15 April 2009 at 09:44 PM

    emm.. interesting )



Home » Articles » Nohup - Run a Command Even Once your Shell is Closed