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 nohup command 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 ssh command:
$ 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 nice command 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 nohup(1) and nice(1) for more information. Please note that nohup is almost available on Solaris/BSD/Linux/UNIX variants.

Other options (suggested by readers)

You can use at command 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 bash(1) 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
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
42 comments… add one
  • Karthik Jul 17, 2016 @ 18:27

    Hi team ,
    please can anyone explain we are RHEL 7.2 for our project
    But our system monitor 19 inches how to correct the resolution,
    sometime display not coming but we can able to access remotely but not for direct graphically login ,please give me a solution .

  • Karthik Jul 17, 2016 @ 18:20

    # nohup nice -n -5 ls / > out.txt &

    Please explain this

  • Karthik Jul 17, 2016 @ 18:14

    nohup > /dev/null 2>&1 Scriptfile.ksh > Scriptlog.log &

    please anyone can explain me about this command

  • polymerase Jan 7, 2016 @ 21:40

    if i log into a server from home
    and use e.g.
    # nohup wget -q -c -O test_fastq.gz “http://addressexample” &
    # exit

    will the (large) download continue to the server without using my home broadband allowance?

    thanks

  • akash Aug 23, 2014 @ 1:47

    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
    #######################################################

    • Babi Dec 7, 2015 @ 7:05

      Just try to run like this:
      /var/root/akash # hohup sh home.sh > main.txt &

  • Chitpreet SIngh Jul 17, 2014 @ 9:22

    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

  • bash Jul 16, 2014 @ 21:19

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

  • Vimal Rathinasamy Dec 14, 2013 @ 7:07

    very clear and precise as usual..thanks..

  • Andy Sep 27, 2013 @ 19:00

    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.

  • Francesco Aug 3, 2012 @ 13:49

    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.

  • mullairamani Jun 6, 2012 @ 8:59

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

  • haberler Nov 16, 2011 @ 17:58

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

  • Richard Aug 25, 2011 @ 23:01

    Thank you very much, excellent article

  • sameer oak Aug 25, 2011 @ 5:34

    > 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 &

  • Carlo Apr 25, 2011 @ 13:43

    Try also.

    Ctrl + z
    bg

    • Derak Oct 17, 2014 @ 5:22

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

  • Mahendra Kumar Mar 29, 2011 @ 6:49

    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. Apr 2, 2011 @ 12:07

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

  • Erlon Dec 22, 2010 @ 21:20

    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??

  • George Anderson Nov 10, 2010 @ 23:40

    Thank you for the disown tip. Very nice article!

    George.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.