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 &
nohup /path/to/command-name arg1 arg2 &
- 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 firstname.lastname@example.org
$ ssh email@example.com
I am going to execute a shell script called pullftp.sh:
# nohup pullftp.sh &
Type exit or press CTRL + D exit from remote server:
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.
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
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.
- See our faq section for disown command examples, jobs command examples, bg command examples, and fg command examples on Linux/Unix process management.
- Man pages - bash(1), ksh(1), nohup(1), at(1)
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop