This guide will show you how to use the command ‘nohup’ to run processes in the background, so that they keep running even if you log out or get disconnected.
I work on remote servers a lot. I use SSH to connect to my servers. Quite often I run a process that I need to let run even after I close my SSH connection. This could be a shell script that parses through my log files or very large mysql database restoration. That’s when I turn to a Linux command line utility called “nohup”. “Nohup” is short for “no hangup”, which translates to “ignore the hang up signal”. The “hang up” signal is what happens when you log out. What “nohup” does is that it allows you to log into your server, launch a process and log out. The process keeps running even after you have logged out. When you log in again you can access that process again, provided its still running.
The basic syntax of “nohup” is:
# nohup [command] &
Replace “[command]” with the name of your shell script, or a command. The “&” at the end makes the command or script run as a background process. Here’s an example of how you would use “nohup” to take a backup of a large mysql database on your remote server. First, I log in to my remote server using SSH:
# ssh firstname.lastname@example.org
Then I execute the command “mysqldump -ucalvin -phobbes largedatabase > largedatabase.db” in the “nohup” mode by adding a “nohup” before and “&” after the command:
# nohup mysqldump -ucalvin -phobbes largedatabase > largedatabase.db &
# nohup: appending output to `nohup.out’
There are two lines of output that you get. The “3999” in the first line is the process ID of the process that I just spawned, while the “nohup: appending output to `nohup.out'” means that the out put that would usually come to the terminal is being forwarded into a file called “nohup.out” in the directory from which you launched the command. You can check if the process is still running by searching through all the running processes using the process ID:
# ps -ef | grep 3999
calvin 3999 29848 0 18:42 ? 00:00:00 /usr/bin/mysqldump
calvin 6575 31852 0 18:44 pts/2 00:00:00 grep 3999
When the process is done running you will see an output in the command line like this:
+ Done nohup mysqldump -ucalvin -phobbes largedatabase > largedatabase.db
You can check the file “nohup.out” to check for any errors or other messages that were output while the process was running. If the file remains blank that’s okay. It means no messages were output while the process was running.
Now that you know how to run processes in the background, why not learn a few other linux tips and tricks?
3 thoughts on “How to Run Processes in the Background Using nohup in Linux”
Thanks for a wonderful post, this helped a lot.
what if one doesn’t want data to go in nohup.out….I am starting my processes as background process but the problem is log statements on terminal which is anyways being written to log file…..nohup is helping in avoiding it to terminal…but still writing in nohup.out…which I don’t want as it is redundant log data which is already there in log files
I recently used the ‘nohup’ command as sugested for an instance of tshark I wanted to run for a long time.
Nohup worked great. Thanks guys.