Red Green Repeat Adventures of a Spec Driven Junkie

UNIX Jobs Handling

Running multiple jobs on a command line interface isn’t intuitive, because there’s only one command prompt.

Tools like tmux and screen are helpful because they give multiple terminals, multiple command prompts.

Before these tools, UNIX systems had a way to have multiple jobs running on a single command prompt, through the tools: jobs, fg, bg, ^z, and &.

jobs lists currently running background jobs
fg brings the last running background job to the foreground
bg start stopped job to run in the background
^z stop the current foreground job
& start the current job in the background

Let’s try these commands out.

sleep Practice

I will use the sleep command to practice. First let’s create job:

$ sleep 10001

At this point, the prompt will freeze, because the sleep command is executing.

Stopping Running Job

To stop the job, enter: ^z:

$ sleep 10001
^Z
[1]+  Stopped                 sleep 10001
$

Resurrecting Stopped Job

To bring this job back to the foreground, run: fg:

$ sleep 10001
^Z
[1]+  Stopped                 sleep 10001
$ fg
sleep 10001

And the job will resume as it was previously.

Resurrecting Stopped Job to Background

To start a stopped job in the background, run: bg:

$ sleep 10001
^Z
[1]+  Stopped                 sleep 10001
$ bg
[1]+ sleep 10001 &
$
sleep 10001

Viewing Background Jobs

Now, the job will run in the background. To see the job running, run job again.

$ sleep 10001
^Z
[1]+  Stopped                 sleep 10001
$ bg
[1]+ sleep 10001 &
$ jobs
[1]+  Running                 sleep 10001 &
$

Starting a Background Job

If you noticed, running bg on a stopped job, basically executed the command with an & appended. Appending & to a command will automatically start the command in the background, for example, if we added another job to the current background job:

$ jobs
[1]+  Running                 sleep 10001 &
$ sleep 10002 &
[2] 50990
$ jobs
[1]-  Running                 sleep 10001 &
[2]+  Running                 sleep 10002 &
$

The system uses the value 50990 to track of running processes.

Running fg will bring the last command, in this instance, sleep 10002.

Job Control

To control a specified job, use its job number:

$ jobs
[1]-  Stopped                 sleep 10001
[2]   Stopped                 sleep 10002
[3]+  Stopped                 sleep 10003
$ bg 2
[2] sleep 10002 &
$ jobs
[1]-  Stopped                 sleep 10001
[2]   Running                 sleep 10002 &
[3]+  Stopped                 sleep 10003
$ fg 1
sleep 10001
^Z
[1]+  Stopped                 sleep 10001
$

Short Cut

Have you noticed some jobs have a + or - next to them? These are indicators:

  • + the default job that command to apply against, like fg
  • - the job that using - option to apply to, like: fg -

Example:

% jobs
[1]   Running                 sleep 10001 &
[2]-  Running                 sleep 10002 &
[3]+  Running                 sleep 10003 &
$ fg -
sleep 10002
^Z
[2]+  Stopped                 sleep 10002
$ bg
[2]+ sleep 10002 &
$

Conclusion

UNIX is a multiprocessing system, even with a single interface such as a command line, one can manage multiple concurrent jobs with ease just using: jobs, fg, bg, ^z, and &.

UNIX surprises me how much is possible on such a simple interface!