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

127 lines
6.1 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.3 Backgrounding and killing jobs</TITLE>
</HEAD>
<BODY>
<meta name="description" value="3.11.3 Backgrounding and killing jobs">
<meta name="keywords" value="gs">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR><A NAME=tex2html3462 HREF="node136.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3460 HREF="node132.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3454 HREF="node134.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3464 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3465 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3463 HREF="node136.html">3.11.4 Stopping and restarting </A>
<B>Up:</B> <A NAME=tex2html3461 HREF="node132.html">3.11 Job Control</A>
<B> Previous:</B> <A NAME=tex2html3455 HREF="node134.html">3.11.2 Foreground and background</A>
<BR> <HR> <P>
<H2><A NAME=SECTION005113000000000000000>3.11.3 Backgrounding and killing jobs</A></H2>
<P>
Let's begin with a simple example. The command <tt>yes</tt> is a
seemingly useless command which sends an endless stream of <tt>y</tt>'s
to standard output. (This is actually useful. If you piped the output
of <tt>yes</tt> to another command which asked a series of yes and no
questions, the stream of <tt>y</tt>'s would confirm all of the questions.)
<P>
Try it out.
<P><TT> /home/larry# <em>yes</em> <BR>
y <BR>
y <BR>
y <BR>
y <BR>
y
<P></TT>
<A NAME=3435>&#160;</A>
<A NAME=3436>&#160;</A>
<A NAME=3437>&#160;</A>
<A NAME=3438>&#160;</A>
The <tt>y</tt>'s will continue <em>ad infinitum</em>. You can kill the process
by hitting your interrupt key, which is usually <IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img193.gif">.
So that we don't have to put up with the annoying stream of <tt>y</tt>'s,
let's redirect the standard output of <tt>yes</tt> to <tt>/dev/null</tt>. As
you may remember, <tt>/dev/null</tt> acts as a ``black hole'' for data.
Any data sent to it will disappear. This is a very effective method of
quieting an otherwise verbose program.
<P><TT> /home/larry# <em>yes <b>&gt;</b> /dev/null</em>
<P></TT>
Ah, much better. Nothing is printed, but the shell prompt doesn't
come back. This is because <tt>yes</tt> is still running, and is sending
those inane <tt>y</tt>'s to <tt>/dev/null</tt>. Again, to kill the job,
hit the interrupt key.
<P>
Let's suppose that we wanted the <tt>yes</tt> command to continue to
run, but wanted to get our shell prompt back to work on other things.
We can put <tt>yes</tt> into the background, which will allow it to
run, but without need for interaction.
<P>
<A NAME=3454>&#160;</A>
<A NAME=3455>&#160;</A>
One way to put a process in the background is to append an ``<tt>&amp;</tt>''
character to the end of the command.
<P><TT> /home/larry# <em>yes <b>&gt;</b> /dev/null &amp;</em> <BR>
<code>[1] 164</code> <BR>
/home/larry#
<P></TT>
As you can see, we have our shell prompt back. But what is this
``<tt><code>[1] 164</code></tt>''? And is the <tt>yes</tt> command really running?
<P>
The ``<tt><code>[1]</code></tt>'' represents the <b>job number</b> for the <tt>yes</tt>
process. The shell assigns a job number to every running job. Because
<tt>yes</tt> is the one and only job that we're currently running, it
is assigned job number <tt>1</tt>. The ``<tt>164</tt>'' is the process ID, or
PID, number given by the system to the job. Either number may be used to
refer to the job, as we'll see later.
<P>
<A NAME=3569>&#160;</A>
You now have the <tt>yes</tt> process running in the background, continuously
sending a stream of <tt>y</tt>'s to <tt>/dev/null</tt>. To check on the
status of this process, use the shell internal command <tt>jobs</tt>.
<P><TT> /home/larry# <em>jobs</em> <BR>
<code>[1]+ Running yes &gt;/dev/null &amp;</code> <BR>
/home/larry#
<P></TT>
Sure enough, there it is. You could also use the <tt>ps</tt> command as
demonstrated above to check on the status of the job.
<P>
<A NAME=3570>&#160;</A>
<A NAME=3478>&#160;</A>
<A NAME=3479>&#160;</A>
To terminate the job, use the command <tt>kill</tt>.
This command takes either a job number or a process ID number as an
argument. This was job number 1, so using the command
<P><TT> /home/larry# <em>kill %1</em>
<P></TT>
will kill the job. When identifying the job with the job number, you must
prefix the number with a percent (``<tt>%</tt>'') character.
<P>
Now that we've killed the job, we can use <tt>jobs</tt> again to check on it:
<P><TT> /home/larry# <em>jobs</em>
<PRE>[1]+ Terminated yes &gt;/dev/null
</PRE>
/home/larry#
<P></TT>
The job is in fact dead, and if we use the <tt>jobs</tt> command again
nothing should be printed.
<P>
You can also kill the job using the process ID (PID) number, which is
printed along with the job ID when you start the job. In our example,
the process ID is 164, so the command
<P><TT> /home/larry# <em>kill 164</em>
<P></TT>
is equivalent to
<P><TT> /home/larry# <em>kill %1</em>
<P></TT>
You don't need to use the ``<tt>%</tt>'' when referring to a job by its
process ID.
<P>
<BR> <HR><A NAME=tex2html3462 HREF="node136.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3460 HREF="node132.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3454 HREF="node134.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3464 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3465 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3463 HREF="node136.html">3.11.4 Stopping and restarting </A>
<B>Up:</B> <A NAME=tex2html3461 HREF="node132.html">3.11 Job Control</A>
<B> Previous:</B> <A NAME=tex2html3455 HREF="node134.html">3.11.2 Foreground and background</A>
<BR> <HR> <P>
<BR> <HR>
<P><ADDRESS>
<I>Matt Welsh <BR>
mdw@sunsite.unc.edu</I>
</ADDRESS>
</BODY>