nohup Execute Commands After You Exit From a Shell Prompt

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:

nohup Syntax:

nohup command-name &

Where,

  • 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.

nohup 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, enter:
# 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.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 17 comments… read them below or add one }

1 Jason Buberel 01.04.06 at 12:28 pm

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 01.04.06 at 7:54 pm

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

3 Stoyan 01.04.06 at 8:38 pm

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

4 LinuxTitli 01.04.06 at 11:39 pm

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 01.05.06 at 8:52 pm

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

6 LinuxTitli 01.06.06 at 12:16 am

Brock, seems good choice, thanks

7 Ruwinda Fernando 03.03.09 at 7:09 am

Thank you. I’ve been searching for this. I knew this kind a functionality should exist. Often i had this issue when ever I’m connecting from home to my office-server.

BR :)

8 anil 04.15.09 at 8:20 pm

Hi everybody.

I am working as sys admin on linux and now i want to learn a programming lanuage can anybody suggest which one i should go for. Which can help me both at admin work and with small app programs.

regards
anil

9 shane 04.28.09 at 2:07 pm

Screen is definitely the best way to do this.

You can run commands and then detach the screen, Ctrl+a+d and leave the command running

10 Justin 04.30.09 at 8:12 am

Thanks. Excellent for those lengthy commits!

11 Dan Grindstaff 06.12.09 at 3:56 pm

I used nohup as follows… nohup ./command &. Did I make a mistake in putting the “./” in front of the command when executing it?

12 Vivek Gite 06.12.09 at 4:05 pm

./ means run command from current directory. Do you have command in current dir?

13 Dan Grindstaff 06.12.09 at 4:10 pm

Thanks for the reply. Yes command is in current directory but nohup log lists several complaints about not being able to find various files. Also, when executing, I was expecting to be returned to the command prompt which I was not.

14 Dan Grindstaff 06.12.09 at 4:54 pm

I just checked with my Unix admin who thinks that it does not matter whether you include the ./ in the nohup string. Also, If I hit return during the process I get my prompt back and can run a monitoring script.

15 Vivek Gite 06.12.09 at 7:05 pm

What kind of error you get in log files? When you get prompt back what do you see with ps and jobs command?

16 Dan Grindstaff 06.12.09 at 8:27 pm

Hi and thanks for the reply. I am seeing error messages that files cannot be found that I know are present in a different folder (where it should be looking). That is what concerned me that I might have forced the process to look only in it’s current directory with “./”. When I get the prompt back I can ps and see the job running.

17 billyduc 07.01.09 at 10:07 am

I have the situation…
I did ssh to the remote server. Run the yum -y update command without nohup!
The updating run for a while and I want to log out off the remote server…and I want yum still run even I log out..
How can I use ‘nohup’ without canceling my update !
thanks for any suggestions !!

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>

Tagged as: , , , , , , , , , , , , , , ,

Previous post: Linux desktop search

Next post: Executing Linux / UNIX commands from web page