Different Ways to Start Processes
Some processes start automatically when the system starts up.
The init scripts, as described in Chapter 6, control these
processes. Each time a command is run, a process is created to
carry out that command. Even then, there is some control over how
the command will be handled.
Starting and Managing Background Jobs
Normally a command is expected to start, do some work, and then
exit gracefully, usually in a short period of time. During that
time, typically nothing else can be done with the shell session
that command was executed, but there is an alternative.
By following a command with an ampersand (&),
that command can be made to run in the background. The shell
reports a job number in brackets and the process number for the
backgrounded command.
$ dbstart
&
[1] 11022
Processing Database instance "TEST": log file
/u01/app/oracle/product/11.1.0/db_1/startup.log
Things can become very confusing if you have a bunch of
background processes writing to your terminal, so it is best to
background processes with little or no output or redirect the
output to a file or other resource using > or >> and a file name.
Now that you have a process running in the background, you
might want to know when it completes. The shell typically reports
a Done message with the job number when a process completes, but
if you want to check what is going on in the meantime, use the
jobs commandto check the
status.
$ jobs
[1]+ Running dbstart
&
The jobs command reports back the job number, status and
command for any processes currently running or stopped in the
background. The status is indicated as Running if the process is
running in the background, Stopped if the process is waiting for
input or has been stopped with the control-z commandor Done if the process has just finished running.
If a process is started normally without an ampersand and there
is a need to either pause the execution or have the process run
in the background instead of canceling the process and restarting
it with an ampersand, use the control-z command to stop the
process and manipulate the job from there.
$ dbshut
control-z
[1]+ Stopped dbshut
$ jobs
[1]+ Stopped dbshut
$ bg 1
[1]+ dbshut &
$ jobs
[1]+ Running dbshut &
Once the job is stopped, it shows up in the job list with a status
of Stopped. To start the job executing in the background, use the
bg command with the job number. As seen above, this changes the
status to Running and the job will continue to run as if it were
started with an ampersand.
Whether a job is stopped or running in the background, it can
be brought back to the foreground with the fg command. This
allows interacting with the program just as if it were started
normally. You get your prompt back when the command completes or
you get tired of waiting and background it again with the
control-z command.
$ dbstart
&
[1] 2599
Processing Database instance "oss": log file
/u01/app/oracle/product/10.2.0/Db_1/startup.log
$ jobs
[1]+ Running dbstart &
$ fg 1
dbstart
$
Here is a table for a quick reference to these job control
commands. Job control can allow you to get multiple things
running at once, but watch out for commands which might interfere
with each other and make sure you can keep track of the output.
Command |
Action |
& |
Append to the end of a command to start it
running in the background |
jobs |
Display all backgrounded jobs whether running
or stopped |
control-z |
Stop the job running in the current shell and
background it |
bg 1 |
Start running job 1 in the background |
fg 1 |
Move job 1 to the foreground, moving it into
the current shell |
Table 10.3: Job Control Commands