Wednesday, May 28, 2008

STOPPED JOBS and job control...

When you try to exit a window or logout, you may get a message:

There are stopped jobs.

This means that a program you were running has been suspended (usually by
typing Ctrl-Z) and has not been properly terminated. If you just try to exit
again, the system will automatically terminate those jobs. If you want to see
what those jobs are, use the 'jobs' command. Just type:

jobs

You will see a listing, which may look like this:

[1] - Stopped foo
[2] + Stopped bar

If you want to continue using one of the jobs in the list, use the 'fg'
command. For example, if you wanted to continue using 'foo' in the above
listing, type this:

fg %1

Actually, the 'fg' part is optional. So, you could just type:

%1

...and get the same effect. If instead you want to just terminate a job, use
the 'kill' command. For example, if you wanted to kill 'bar' in the above
listing, type this:

kill %2

The information below describes jobs in much more detail, if you're
interested.

The shell associates a job with each pipeline (most often a single
command/program) It keeps a table of current jobs, printed by the "jobs"
command, and assigns them small integer numbers. When a job is started
asynchronously with '&' (started in the background), the shell prints a line
which looks like:

[1] 1234

indicating that the job that was started asynchronously was job number 1 and
had one (top-level) process, whose process ID number was 1234.

If you are running a job and wish to do something else, you may type Ctrl-Z,
which sends a STOP signal to the current job. The shell will then normally
indicate that the job has been 'Stopped', and print another prompt. You can
then manipulate the state of this job, putting it in the background with the
'bg' command, or run some other commands and then eventually bring the job
back into the foreground with the foreground command 'fg'. A Ctrl-Z takes
effect immediately and is like an interrupt in that pending output and unread
input are discarded when it is typed. There is another special key, Ctrl-Y,
which does not generate a STOP signal until a program attempts to read(2) it.
This can be typed ahead when you have prepared some commands for a job which
you wish to stop after it has read them.

A job being run in the background will stop if it tries to read from the
terminal. Background jobs are normally allowed to produce output, but this
can be disabled by giving the command 'stty tostop'. If you set this tty
option, then background jobs will stop when they try to produce output like
they do when they try to read input.

There are several ways to refer to jobs in the shell. The character '%'
introduces a job name. If you wish to refer to job number 1, you can name it
as '%1'. Just naming a job brings it to the foreground; thus '%1' is a
synonym for 'fg %1', bringing job 1 back into the foreground. Similarly
saying '%1 &' resumes job 1 in the background. Jobs can also be named by
prefixes of the string typed in to start them, if these prefixes are
unambiguous, thus '%ex' would normally restart a suspended ex(1) job, if there
were only one suspended job whose name began with the string 'ex'. It is also
possible to say '%?string' which specifies a job whose text contains string,
if there is only one such job.

The shell maintains a notion of the current and previous jobs. In output
pertaining to jobs, the current job is marked with a '+' and the previous job
with a '-'. The abbreviation '%+' refers to the current job and '%-' refers
to the previous job. For close analogy with the syntax of the history
mechanism, '%%' is also a synonym for the current job.