Detach a running process – Linux

Wednesday, 31 de August 2011 — Bruno Lucas

Pub.

Sometimes I start a job remotely and only after I remember that I may need to log-off, but the process must continue to run afterwards. This can be done with command disown. On the same shell where your process is running type Ctrl+Z, to pause the program, you’ll see the job id, something like [1], you need it for the disown command, now restart the job in background with the command bg and you’re ready to issue the disown command:

disown -h %1

you can also use the pid1 of the process (use jobs -l on that same shell and you’ll get a longer number that is the pid of the process you want to detach):

disown -h pid

after that you can close the shell with exit or Ctrl+D.

If you start your program with nohup2 or screen you don’t have to worry about log-off and running processes.

This command can also be used in Unix (such as BSD/MacOS X/etc.) but their might be implementation differences.

1 After some interaction on Google+ with CNF we found out that the pid is accepted but it is not documented. I’ve changed the post to reflect a more appropriated use of the disown command (according to the manual).