≡ Menu

nohup Execute Commands After You Exit From a Shell Prompt

Most of the time you login into remote server via ssh. If you start a shell script or command and you exit (abort remote connection), the process / command will get killed. Sometime job or command takes a long time. If you are not sure when the job will finish, then it is better to leave job running in background. But, if you log out of the system, the job will be stopped and terminated by your shell. What do you do to keep job running in the background when process gets SIGHUP?

Say hello to nohup command

The answer is simple, use [nixcmd name="nohup"] line-utility which allows to run command/process or shell script that can continue running in the background after you log out from a shell:

nohup command syntax:

The syntax is as follows
nohup command-name &
OR
nohup /path/to/command-name arg1 arg2 &

Where,

  • command-name : is name of shell script or command name. You can pass argument to command or a shell script.
  • & : nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an & symbol.

Use jobs -l command to list all jobs:
# jobs -l

nohup command examples

First, login to remote server using [nixcmd name="ssh"]:
$ ssh user@remote.server.com
OR
$ ssh vivek@server1.cyberciti.biz

I am going to execute a shell script called pullftp.sh:
# nohup pullftp.sh &

Type exit or press CTRL + D exit from remote server:
# exit

In this example, I am going to find all programs and scripts with setuid bit set on, enter:
# nohup find / -xdev -type f -perm +u=s -print > out.txt &

Type exit or press CTRL + D exit from remote server.
# exit

Please note that nohup does not change the scheduling priority of COMMAND; use [nixcmd name="nice"] for that purpose. The syntax is:
# nohup nice -n -5 ls / > out.txt &
As you can see nohup keep processes running after you exit from a shell. Read man page of [man]nohup(1)[/man] and [man]nice(1)[/man] for more information. Please note that nohup is almost available on Solaris/BSD/Linux/UNIX variants.

Other options (suggested by readers)

You can use [nixcmd name="at"] to queue a job for later execution. For example, you can run pullftp.sh script to queue (one minute) later execution:
$ echo "pullftp.sh" | at now + 1 minute
You can also use screen command for same. The disown shell internal command for same purpose. Here is how you can try it out:
$ pullftp.sh &
$ disown -h
$ exit

From the bash [man]bash(1)[/man] man page:

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.

Recommended reading

Comments on this entry are closed.

  • Jason Buberel January 4, 2006, 12:28 pm

    You forget the other easy way to do the same: the ‘at’ command!

    > echo “pullftp.sh” | at now + 1 minute

    Then logout. After the job completes, you’ll even get an email message with the contents of stdout/stderr.

  • LinuxTitli January 4, 2006, 7:54 pm

    Jason thanks for pointing out at command :) appreciate your post.

  • Stoyan January 4, 2006, 8:38 pm

    I have screen installed on all servers. Not only detaching (nohup), but also opening new consoles etc.

  • LinuxTitli January 4, 2006, 11:39 pm

    Heh, my bad.

    Stoyan thanks for reminding us about screen manager :) I will update post tomorrow (it is late night now) with screen utility.

  • Brock Tice January 5, 2006, 8:52 pm

    Screen is good, but there’s also “disown” in bash, which leaves jobs running even if you log out.

  • LinuxTitli January 6, 2006, 12:16 am

    Brock, seems good choice, thanks

  • Ruwinda Fernando March 3, 2009, 7:09 am

    Thank you. I’ve been searching for this. I knew this kind a functionality should exist. Often i had this issue when ever I’m connecting from home to my office-server.

    BR :)

  • anil April 15, 2009, 8:20 pm

    Hi everybody.

    I am working as sys admin on linux and now i want to learn a programming lanuage can anybody suggest which one i should go for. Which can help me both at admin work and with small app programs.

    regards
    anil

  • shane April 28, 2009, 2:07 pm

    Screen is definitely the best way to do this.

    You can run commands and then detach the screen, Ctrl+a+d and leave the command running

  • Justin April 30, 2009, 8:12 am

    Thanks. Excellent for those lengthy commits!

  • Dan Grindstaff June 12, 2009, 3:56 pm

    I used nohup as follows… nohup ./command &. Did I make a mistake in putting the “./” in front of the command when executing it?

    • nixCraft June 12, 2009, 4:05 pm

      ./ means run command from current directory. Do you have command in current dir?

  • Dan Grindstaff June 12, 2009, 4:10 pm

    Thanks for the reply. Yes command is in current directory but nohup log lists several complaints about not being able to find various files. Also, when executing, I was expecting to be returned to the command prompt which I was not.

  • Dan Grindstaff June 12, 2009, 4:54 pm

    I just checked with my Unix admin who thinks that it does not matter whether you include the ./ in the nohup string. Also, If I hit return during the process I get my prompt back and can run a monitoring script.

  • nixCraft June 12, 2009, 7:05 pm

    What kind of error you get in log files? When you get prompt back what do you see with ps and jobs command?

  • Dan Grindstaff June 12, 2009, 8:27 pm

    Hi and thanks for the reply. I am seeing error messages that files cannot be found that I know are present in a different folder (where it should be looking). That is what concerned me that I might have forced the process to look only in it’s current directory with “./”. When I get the prompt back I can ps and see the job running.

  • billyduc July 1, 2009, 10:07 am

    I have the situation…
    I did ssh to the remote server. Run the yum -y update command without nohup!
    The updating run for a while and I want to log out off the remote server…and I want yum still run even I log out..
    How can I use ‘nohup’ without canceling my update !
    thanks for any suggestions !!

  • Solaris July 12, 2009, 3:21 pm

    I found these two methods googling:

    Method 1:
    – start the process from console
    – send it to background CTRL + Z
    – then disown -a

    Method 2:
    nohup command

  • Russell June 9, 2010, 9:53 pm

    Thanks! I needed a shell script to run a program, without it closing as soon as the script was over!

  • ashah July 2, 2010, 4:36 pm

    Question: I started a nohup foo & . Now I want to stop it… is there a way to end the process even though I ‘nohupped’ it prior?

    The process had an error and stopped running, but I am still logged into the server even after I close my SSH terminal.

    Thanks in advance!

  • Aggdata September 3, 2010, 4:28 pm

    Hi There
    My nohup keeps ending with “Cancel request sent”

    nohup Scriptfile.ksh > Scriptlog.log 2>&1 &

    this happens even when I nohup it as you can see above

    Any help would be much appreciated

  • George Anderson November 10, 2010, 11:40 pm

    Thank you for the disown tip. Very nice article!

    George.

  • Erlon December 22, 2010, 9:20 pm

    How can a trigger a remote command via ssh command line??
    Like:
    ssh root@machine “nohup /home/test/scripts/qemu-vio &”

    In the command above, the program qemu-vio doent run in backgroung. Why?? How can I do that??

  • Mahendra Kumar March 29, 2011, 6:49 am

    Could any one tell me what are the expected outputs of a nohup command ? If any script / job fails or successful, what are the outcome of this command ?

    • Jair L. April 2, 2011, 12:07 pm

      You may find the results of the executed script or command executed with nohup, visiting nohup.out file generated automatically.

  • Carlo April 25, 2011, 1:43 pm

    Try also.

    Ctrl + z
    bg

    • Derak October 17, 2014, 5:22 am

      That puts the script in the background of the current session. If you disconnect, the script will terminate.

  • sameer oak August 25, 2011, 5:34 am

    > Aggdata September 3, 2010
    > Hi There
    > My nohup keeps ending with “Cancel request sent”
    > nohup Scriptfile.ksh > Scriptlog.log 2>&1 &
    > this happens even when I nohup it as you can see above
    > Any help would be much appreciated
    [ sameeroak ]
    try the following:
    nohup > /dev/null 2>&1 Scriptfile.ksh > Scriptlog.log &

  • Richard August 25, 2011, 11:01 pm

    Thank you very much, excellent article

  • haberler November 16, 2011, 5:58 pm

    is it available in centos dist’s ? l will search for more manuals :) thanks for sharing

  • mullairamani June 6, 2012, 8:59 am

    is it possible to use the news command in single system in linux

  • Francesco August 3, 2012, 1:49 pm

    Hi everybody.
    I have launched a set of commands with the nohup features. If i launch “top” i have 4 processes running in the background, all having the same name.
    Is there a way i can go back to the command line that launched each of the processes?
    To be correct, i have written a small bash file that launches all the programs one aftert the other.

    so what i’ve done is:

    chmod +x bash.sh
    nohup ./bash.sh >/dev/null &

    I have 4 different files, each launching a set of processes having all the same name. I need to recall only one of this bash files because i ve noticed i gave a wrong input.
    thanks for the help,

    F.

  • Andy September 27, 2013, 7:00 pm

    Thanks, just what I needed!

    It happens often that I’m too lazy to use the GUI and so I’d simply hack in a

    $ iceweasel &>/dev/null &

    on my Debian to have an iceweasel running without giving this pointless debug output crap.
    HOWEVER, beware if you have an (l)xterm with 2 tabs, and this is the 1st tab! You might mess up this sub-terminal completely, and close it, leaving only the 2nd tab open!
    Of course, you guessed it — iceweasel will be KILLED.
    nohup can avoid that. I presume that the ‘Run’ command in LXDE dropdown menu does a nohup under the veil, too.

  • Vimal Rathinasamy December 14, 2013, 7:07 am

    very clear and precise as usual..thanks..

  • bash July 16, 2014, 9:19 pm

    Don’t forget `setsid ls -l` or just “( ls -l ) &”

  • Chitpreet SIngh July 17, 2014, 9:22 am

    Hello All

    Can anyone tell how to stop if I execute my command line using nohup .
    If by mistake if have input wrong command i want to exit from backround execution or nohup commad , How to do it,Kindly let me know

  • akash August 23, 2014, 1:47 am

    nohup pullftp.sh &
    i m trying to execute this command but unable to execute , why ?

    please tell me the exect way to run a command
    ######################################################
    /var/root/akash # sh home.sh
    45
    70
    90
    160
    /var/root/akash # nohup home.sh>main.txt &
    /var/root/akash # cat main.txt
    nohup: can’t execute ‘home.sh': No such file or directory
    [1]+ Done(127) nohup home.sh 1>main.txt
    #######################################################