Running process in the background with Nohup

by Sukrit Dhandhania on December 22, 2008

Linux

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 calvin@sevenacross.com

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 &
[1] 3999
# 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:

[1]+ 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.

Related Posts:
  • How to use dstat to monitor your Linux/UNIX server
  • How to get detailed information on each running process in Windows
  • How to use a screensaver as your desktop background in OS X
  • How to reset a lost MySQL root password
  • How to blurminal your OS X Terminal
  • Get Simple Help tutorials just like this one in your email inbox every day - for free! Just enter your email address below:

     

    You can always opt out of this email subscription at any time.

    { 1 comment… read it below or add one }

    1 Bobby Cox 12.22.08 at 8:44 am

    Hello - Nohup is useful, but if you use screen you can detach and reattach to a terminal at will and have commands running in the background as well.

    http://www.gnu.org/software/screen/


    Bobby

    Leave a Comment

    You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>