Cogs and Levers A blog full of technical stuff

Detaching running processes in bash

There are quite a few times where I’ve run a command on a remote machine and needed to get out of that machine but leave my command running.

I’ll normally start a job that I know is going to take a while using an ampersand like so:

$ long-running-prog &

Really, the nohup command should also be put on the command line so that the command that you execute will ignore the signal SIGHUP.

$ nohup long-running-prog &
$ exit

If you’re already part-way through a running process, you can get it to continue running in the background (while you make your getaway) by doing the following

$ long-running-prog
CTRL-Z
$ bg
$ disown pid

You use CTRL-Z to suspend the running process. The bg command then gets the program running in the background. You can confirm that it is running in the background with the jobs command. Lastly, using disown detatches the process running in the background from your terminal, so that when you exit your session the process will continue.

The LDP has a great article on job control.