Pete Freitag Pete Freitag

Unix Job processing

Updated on October 11, 2021
By Pete Freitag

I learned something new from my brother the other day about unix job processing. We had written a script to do some specific analysis of the logs for a site I was workign on, and it would take about 30 seconds to a minute for the script to complete.

I had known for quite some time that you can run a script in the background by appending an & to the end of your command, for instance:

$> ./ &

I often run scripts that take a while, but forget to append the & so they run in the background. Well it turns out you can hit Ctrl+Z this will pause the process that is currently running, and give you a shell. Now you can type bg to send that process into the background. Now you can use the jobs command to see how its doing, or the fg command to send it back into the foreground.

So for example you might do something like this:

$> ./
hits Ctrl+Z
[1]+  Stopped   ./
$> bg
[1]+ ./ &
$> jobs
[1]+  Running   ./ &
$> jobs
[1]+  Done   ./

unix shell bash jobs bg fg

Unix Job processing was first published on November 28, 2005.

If you like reading about unix, shell, bash, jobs, bg, or fg then you might also like:

Weekly Security Advisories Email

Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).


You can also background multiple processes, and recover them individually

hits Ctrl+Z
[1]+ Stopped ./
hits Ctrl+Z on another process
[2]+ Stopped ./

Now you can recover them individually
$ fg 1
$ fg 2
by jehiah on 11/29/2005 at 12:20:36 AM UTC
You should also check into screen if you'd like to play with background processes some more and so forth:

Basically you can use it to start processes in different virtual terminals and detach from them to new terminals or just logout altogether. I used to use screen way back when to run IRC bots... I could start one in a screen, detach from it, and logout of my shell and it would still be running.

Actually, IIRC, you can also start background processes which will still run after you logout by prepending a 'nohup' in front of them. ie: 'nohup ./script &'. However screen gives you the ability to reattach to the process if you need to interact with it, check it's progress, etc.
by Brandon Harper on 11/29/2005 at 4:29:17 AM UTC
Cool tips Jehiah and Brandon - Thanks!
by Pete Freitag on 11/29/2005 at 9:40:03 AM UTC

Stuff like this can certainly come in handy.
by Yves on 11/29/2005 at 10:58:21 AM UTC