Background a running process
Everyone knows and loves screen for running longtime scripts in the background without worrying that the ssh connection will drop and will have to run it again. Still, I have found myself many times in the situation where I started a process and needed to put it in the background and run something else on the console. Uff.. If only I started it with screen. But wait, there is hope. This quick tip will show how to put a process in the background and then start it back in foreground.
This works in bash and uses the ‘suspend‘ key (CTRL+Z) and the bg – background and fg – foreground commands. Let’s say we were running an intensive rsync command, and are wanted to check if we still have the available space on the disk without opening a new ssh session (yes, I know):
rsync -ar server:/source/ /destination/
^Z
Stopped
Let it run in the background:
bg
[1] rsync -ar server:/source/ /destination/ &
Now we can run some other commands like du:
du -h
We can see the background process with ps or jobs:
jobs
[1] Running rsync -ar server:/source/ /destination/
And finally we can bring it back to foreground with fg:
fg
Note: this works only on the running ssh/bash session and it will be closed once you exit. Logout should warn about open/running jobs and that they will be lost if exit.
>
Tags: bash, screen, tips, Tips & Tricks

17th August 2010, 22:55
you can keep it running in the background after logout:
$ disown %1
that lets your backgrounded app run after you logged out. kind of like nohub.
17th August 2010, 22:58
Yes, but how do you move that process into a screen session so that you can disconnect and reconnect from home?
That is the magic that I’m looking for.
17th August 2010, 23:01
yeah cant do that, but often you start something realize you didnt run the thing in screen, then being able to disown is useful. mostly i always run screen
17th August 2010, 23:24
Apparently http://caca.zoy.org/wiki/neercs can grab a running process under its control like screen but if you forgot to screen it.
nice.
17th August 2010, 23:31
@R.I.Pienaar nice tip about disown. Unfortunately once you do that you can’t get it back. But even so it can be useful in some cases.
neercs looks interesting; never heard of it until now, but I will definitely check it out. ‘Interesting’ naming on their projects…
18th August 2010, 00:05
Also check out retty: http://pasky.or.cz/~pasky/dev/retty/
“retty is a tiny tool that lets you attach processes running on other terminals. So you were running that mutt outside of screen at your home machine and now wanna check your mail? Attach it with retty, do whatever you want, detach it again and everything is as it was before. You don’t have to run them all in screen just in case.”
18th August 2010, 00:06
(Sorry scanned the input form too quickly and thought the “website” field was for a title.)
18th August 2010, 00:10
@embobo: no worries; just fixed it. retty sounds interesting and it is also in debian base. I will check it out. Thanks for the tip.