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:
At this point, the prompt will freeze, because the sleep command is executing.
Stopping Running Job
To stop the job, enter: ^z
:
Resurrecting Stopped Job
To bring this job back to the foreground, run: fg
:
And the job will resume as it was previously.
Resurrecting Stopped Job to Background
To start a stopped job in the background, run: bg
:
Viewing Background Jobs
Now, the job will run in the background. To see the job running, run
job
again.
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:
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:
Short Cut
Have you noticed some jobs have a +
or -
next to them? These are
indicators:
+
the default job that command to apply against, likefg
-
the job that using-
option to apply to, like:fg -
Example:
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!