Run a Background Unix Process (even if user is disconnected)

Given we have a script test.sh, it is easy to run this script in the background while continuing to work in the current shell session. The most cited way is by adding ‘&’ to the end of the command:

./test.sh &

However, this approach has one major disadvantage: When the shell session of the user is disconnected or ended in any way, the script we have started in the background will cease to work.Luckily, it is quite easy to start a process in a way, which will prevent the process from ending upon an error with the current user’s session: meet nohup. Using the nohup command, we can start the background script as follows …

nohub ./test.sh &

… the the process will continue to run even if the shell session ends.

Depending on your OS it can be a bit tricky to ‘find’ the process again. You may want to try the following commands to get the process id of your nohub process:

ps -A
sudo ps -A

Note: Depending on your use case, it might be a better idea to install a UNIX daemon instead of using nohub.

Resources

Unix Nohup: Run a Command or Shell-Script Even after You Logout

nohup(1) – Linux man page

stackoverflow.com: What’s the difference between nohup and a daemon?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s