Files
oldlinux-files/docs/Install-Guide/install-guide-2.2.2/node136.html
2024-02-19 00:23:35 -05:00

135 lines
6.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 95.1 (Fri Jan 20 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>3.11.4 Stopping and restarting jobs</TITLE>
</HEAD>
<BODY>
<meta name="description" value="3.11.4 Stopping and restarting jobs">
<meta name="keywords" value="gs">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR><A NAME=tex2html3472 HREF="node137.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3470 HREF="node132.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3466 HREF="node135.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3474 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3475 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3473 HREF="node137.html">3.12 Using the vi </A>
<B>Up:</B> <A NAME=tex2html3471 HREF="node132.html">3.11 Job Control</A>
<B> Previous:</B> <A NAME=tex2html3467 HREF="node135.html">3.11.3 Backgrounding and killing </A>
<BR> <HR> <P>
<H2><A NAME=SECTION005114000000000000000>3.11.4 Stopping and restarting jobs</A></H2>
<P>
<A NAME=3498>&#160;</A>
There is another way to put a job into the background. You can
start the job normally (in the foreground), <b>stop</b> the job,
and then restart it in the background.
<P>
First, start the <tt>yes</tt> process in the foreground, as you
normally would:
<P><TT> /home/larry# <em>yes <b>&gt;</b> /dev/null</em>
<P></TT>
Again, because <tt>yes</tt> is running in the foreground, you shouldn't get
your shell prompt back.
<P>
Now, instead of interrupting the job with <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img194.gif">, we'll <em>suspend</em>
the job. Suspending a job doesn't kill it: it only temporarily stops the
job until you restart it. To do this, you hit the suspend key, which
is usually <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img195.gif">.
<P><TT> /home/larry# <em>yes <b>&gt;</b> /dev/null</em> <BR>
<IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img196.gif"> <BR>
<code>[1]+ Stopped yes &gt;/dev/null</code> <BR>
/home/larry#
<P></TT>
While the job is suspended, it's simply not running. No CPU time is
used for the job. However, you can
restart the job, which will cause the job to run again as if nothing ever
happened. It will continue to run where it left off.
<P>
<A NAME=3571>&#160;</A>
<A NAME=3513>&#160;</A>
To restart the job in the foreground, use the command
<tt>fg</tt> (for ``foreground'').
<P><TT> /home/larry# <em>fg</em> <BR>
yes &gt;/dev/null
<P></TT>
<A NAME=3518>&#160;</A>
<A NAME=3572>&#160;</A>
The shell prints the name of the command again so you're aware of which job
you just put into the foreground. Stop the job again, with <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img197.gif">.
This time, use the command <tt>bg</tt> to put the job into the background.
This will cause the command to run just as if you started the command
with ``<tt>&amp;</tt>'' as in the last section.
<P><TT> /home/larry# <tt>bg</tt> <BR>
<code>[1]+</code> yes &gt;/dev/null &amp; <BR>
/home/larry#
<P></TT>
And we have our prompt back. <tt>jobs</tt> should report that <tt>yes</tt>
is indeed running, and we can kill the job with <tt>kill</tt> as we
did before.
<P>
How can we stop the job again? Using <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img198.gif"> won't work, because
the job is in the background. The answer is to put the job in the
foreground, with <tt>fg</tt>, and then stop it. As it turns out you can use
<tt>fg</tt> on either stopped jobs or jobs in the background.
<P>
There is a big difference between a job in the background and a job
which is stopped. A stopped job is not running---it's not using any
CPU time, and it's not doing any work (the job still occupies
system memory, although it may be swapped out to disk). A job in the
background is running, and using memory, as well as completing some task
while you
do other work. However, a job in the background may try to display text on
to your terminal, which can be annoying if you're trying to work on
something else. For example, if you used the command
<P><TT> /home/larry# <em>yes &amp;</em>
<P></TT>
without redirecting stdout to <tt>/dev/null</tt>, a stream of <tt>y</tt>'s
would be printed to your screen, without any way of interrupting it (you
can't use <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img199.gif"> to interrupt jobs in the background).
In order to stop the endless <tt>y</tt>'s, you'd have to use the <tt>fg</tt>
command, to bring the job to the foreground, and then use <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img200.gif">
to kill it.
<P>
Another note. The <tt>fg</tt> and <tt>bg</tt> commands normally foreground
or background the job which was last stopped (indicated by a
``<tt>+</tt>'' next to the job number when you use the command <tt>jobs</tt>).
If you are running multiple jobs at once, you can foreground or
background a specific job by giving the job ID as
an argument to <tt>fg</tt> or <tt>bg</tt>, as in
<P><TT> /home/larry# <em>fg %2</em>
<P></TT>
(to foreground job number 2), or
<P><TT> /home/larry# <em>bg %3</em>
<P></TT>
(to background job number 3). You can't use process ID numbers with
<tt>fg</tt> or <tt>bg</tt>.
<P>
Furthermore, using the job number alone, as in
<P><TT> /home/larry# <em>%2</em>
<P></TT>
is equivalent to
<P><TT> /home/larry# <em>fg %2</em>
<P></TT>
<P>
Just remember that using job control is a feature of the shell. The
commands <tt>fg</tt>, <tt>bg</tt> and <tt>jobs</tt> are internal to the shell.
If for some reason you use a shell which does not support job control,
don't expect to find these commands available.
<P>
In addition, there are some aspects of job control which differ between
Bash and Tcsh. In fact, some shells don't provide job control at
all---however, most shells available for Linux support job control.
<P>
<A NAME=3564>&#160;</A>
<P>
<P>
<BR> <HR><A NAME=tex2html3472 HREF="node137.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3470 HREF="node132.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3466 HREF="node135.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3474 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3475 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3473 HREF="node137.html">3.12 Using the vi </A>
<B>Up:</B> <A NAME=tex2html3471 HREF="node132.html">3.11 Job Control</A>
<B> Previous:</B> <A NAME=tex2html3467 HREF="node135.html">3.11.3 Backgrounding and killing </A>
<BR> <HR> <P>
<BR> <HR>
<P><ADDRESS>
<I>Matt Welsh <BR>
mdw@sunsite.unc.edu</I>
</ADDRESS>
</BODY>