Detaching a running process on *nix (or how to make a process continue to run after logging out)

Today, I had to copy 70 GiB of data from a ext3 filesystem to a XFS filesystem. This involved a lot of small files. After a couple of hours of waiting, I thought it’d be best to just leave it running, and resume my activities the day after. But oh nooo, I forgot to run it in a screen.

Detaching a running process from your shell, proved to be very easy in deed, using a shell command called disown.

To disown a running process, start by suspending the process by pressing ^Z, then send it to the background with the command bg then you can make it run until it’s done by using the disown command. To access the processes you’ve got suspended, you can use the % prefix (%1 for the first, %2 for the second etc.). To get a list of running processes you can use the jobs command.

Example:

mads@workmads ~ % sleep 120
^Z
zsh: suspended  sleep 120
mads@workmads ~ % bg
[1]  + continued  sleep 120
mads@workmads ~ % disown %1
 
mads@workmads ~ % ps xawu|grep sleep
mads     31725   0.0  0.0    75332    304 s003  S     2:25PM   0:00.00 sleep 120

If you accedently didn’t background your process, you can tell it to resume by sending it a SIGCONT signal with kill -SIGCONT <pid>.

Of course, this is no replacement for nohup or screen, but I think it is a great supplement.

This entry was posted in Work. Bookmark the permalink.

7 Responses to Detaching a running process on *nix (or how to make a process continue to run after logging out)

  1. bsdpunk says:

    Why not just use, screen?

  2. I don’t really know any way to make a already started process go into a screen – if you know of such a function, I’d love to know of it :)

  3. bsdpunk says:

    I’m sorry I thought you could ctrl-z the task then enter screen and bring it back with fg. I appear to be wrong.

  4. That’s both cool and very scary at the same time.

    Thanks for the link though!

  5. Almir Karic says:

    http://pasky.or.cz/~pasky/dev/retty/

    this allows you to attach something already running in a screen session :)

  6. Thomas Habets says:

    Almir:

    As I say in my blog post (http://blog.habets.pp.se/2009/03/Moving-a-process-to-another-terminal mentioned above):

    “There is a program called retty that I found later on that sort of does this, but it only closes and re-opens stdin/out et al. It doesn’t seem to do full terminal handling. Nor does it seem to detach the original terminal. It only allows you to peek into the process, control it for a bit, and then hand it back. If you shut down the original terminal you’re still screwed.”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may 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> <pre lang="" line="" escaped="">