Job Control for Linux / Ubuntu Shell

2014-03-17 | #helper, #ubuntu

Long running scripts can be a bitch, especially if you need to leave the shell somewhere along the way. But the basic management of jobs is actually quite basic and convenient.

First and foremost: Use CTRL-Z to put the current foreground job to sleep. The resulting output tells you the job id in brackets at the beginning ([1]+). This is the one you want to use, if any.

Three basic commands:

fg %1 //return job 1 to foreground execution

bg %1 //put job 1 into the background, making the shell interactive again, while still getting output

jobs //get a list of all current jobs for the active shell

With these 3 + 1 commands you can switch and control running jobs easily.

A special condition might occur if you want to close the shell and still keep the a current job running. If you know this before starting the script you should use "nohup", which is short for "no hang up" and tells the job to keep running if the shell is terminated.

nohup my_mighty_script

If the job is already running you can activate this behaviour on the fly by putting the job to sleep, putting it in the background and then "disowning" it from the shell, by the nicely named command "disown".

disown -h %1 //tell job one to keep going even if the shell is closed