About nixCraft

Topics

nohup execute commands after you exit from a shell prompt

Posted by Vivek Gite [Last updated: August 27, 2007]

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. However, if you logout the system, the job will be stopped. What do you do?

nohup command

Answer is simple, use nohup utility which allows to run command./process or shell script that can continue running in the background after you log out from a shell:

Syntax:

nohup command-name &

Where,

nohum command examples

1) Login to remote server

$ ssh user@remote.server.com

2) Execute script called pullftp.sh

# nohup pullftp.sh &

Type exit or press CTRL + D exit from remote server.

# exit

3) Find all programs and scripts with setuid bit set on

# 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 for that:

# 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 and nice command for more information. Please note that nohup is almost available on Solaris/BSD/Linux/UNIX variant.

Update:
# 1: As pointed out by Jason 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

# 2: You can also use screen command for same. Brock pointed out disown shell internal command for same purpose. Here is how you can try it out:

$ pullftp.sh &
$ disown -h
$ exit

According to bash 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.

Tell us how we're doing: Please answer a few questions about your experience to help us improve nixCraft.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Jason Buberel Says:

    You forget the other easy way to do the same: the ‘at’ command!

    > echo “pullftp.sh” | at now + 1 minute

    Then logout. After the job completes, you’ll even get an email message with the contents of stdout/stderr.

  2. LinuxTitli Says:

    Jason thanks for pointing out at command :) appreciate your post.

  3. Stoyan Says:

    I have screen installed on all servers. Not only detaching (nohup), but also opening new consoles etc.

  4. LinuxTitli Says:

    Heh, my bad.

    Stoyan thanks for reminding us about screen manager :) I will update post tomorrow (it is late night now) with screen utility.

  5. Brock Tice Says:

    Screen is good, but there’s also “disown” in bash, which leaves jobs running even if you log out.

  6. LinuxTitli Says:

    Brock, seems good choice, thanks

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

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

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.