Tuesday Tiny Techie Tip
Background tasks in csh(1)
UNIX is a multi-tasking operating system, so multiple jobs
can be active at the same time. In modern window-oriented
user interfaces, we usually take advantage of this fact by
just starting a new window when current windows are busy
doing other things and we want to start a new process. But
if you have processes which do not require human
intervention, you can force them to run in the background to
avoid tying up your shell with a long-running task.
To send a task into the background from csh and its
brethren, you append the control character "&" to
the command line like this:
% alongcommand &
[1] 12593
%
The "[1] 12593" line indicates that this is the first
background task in the current shell, and the process ID of the
task is 12593.
If your process generates output to stdout or stderr, the
output will still show up on your terminal (unless you've
done "stty tostop"), so if the output is important,
you should redirect it to a file so it isn't lost in the
shuffle of whatever else you work on after the task has been
spawned.
If your job blocks for input, the job will be stopped and
you'll get a message like:
[1] + Stopped (tty input) alongcommand
%
To bring a backgrounded task back into the foreground, use
the "fg(1)" command. This command accepts a job
number prefaced by a percent sign ("%") (the
kill shell built-in command will also accept this
"%jobnumber" syntax).
% fg %1
alongcommand
Note that the shell echoes the name of the command brought to
the foreground. After foregrounding a task, the terminal is
reattached to that task, so anything you type will be sent
to the task's stdin as if it had never been in the
background. To return the task to the background, you first
have to get back to the shell by suspending the task with
the suspend character ("Ctrl-Z" unless you've
changed it)
^Z
Stopped
%
Once the command is stopped and you have control of the shell
back, you use the jobs(1) command to check the job
number:
% jobs
[1] + Stopped alongcommand
%
Once you know the job number you can send it back into the
background with the bg(1) command giving the job
number prefaced by a percent sign as with fg
% bg %1
[1] alongcommand &
%
Again, the shell tells you that the command has been placed
in the background.
The shell maintains the concept of a current job which is
indicated with a plus sign ("+") in the
jobs output. When operating on the current job,
you can leave off the job number. The act of suspending a
task with Ctrl-Z makes that task the current job,
so you can skip looking up the job number if you're sending
a newly-suspended job to the background.
% alongcommand
^Z
Stopped
% bg
[1] alongcommand &
%
When a job completes, you'll get a message at the shell like this:
[1] Done alongcommand
%
If you have jobs running in the background of a shell and you exit
the shell, the jobs will continue to run until they complete or block
for input. There is no way to reattach to a backgrounded job once the
shell which spawned it has exited.
If you have stopped jobs, the shell will warn you about them if you
try to exit:
% exit
There are stopped jobs.
%
But beware, it will only warn you once, so if you exit after the
warning, the stopped jobs will be killed.
Tuesday Tiny Techie Tip -- 17 June 1997
This is the last (chronologically) TTTT.
Loop Back to (10/29/96) (the first
TTTT.)
Back to (06/03/97)
Written by Jeff Youngstrom
Up to the TTTT index
Tuesday Tiny Techie Tips are all © Copyright
1996-1997 by Jeff Youngstrom. Please ask permission before
reproducing any of this material.