⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12_01.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 12] Job Control</TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:36:24Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="part02.htm"TITLE="II. Let the Computer Do the Dirty Work "><LINKREL="prev"HREF="ch11_16.htm"TITLE="11.16 Instead of Changing History Characters "><LINKREL="next"HREF="ch12_02.htm"TITLE="12.2 Other Ways to Refer to Jobs "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch11_16.htm"TITLE="11.16 Instead of Changing History Characters "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 11.16 Instead of Changing History Characters "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 12</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch12_02.htm"TITLE="12.2 Other Ways to Refer to Jobs "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 12.2 Other Ways to Refer to Jobs "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-12">12. Job Control</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-0439"TITLE="12.1 Job Control: Work Faster, Stop Runaway Jobs ">Job Control: Work Faster, Stop Runaway Jobs </A><BR><ACLASS="sect1"HREF="ch12_02.htm"TITLE="12.2 Other Ways to Refer to Jobs ">Other Ways to Refer to Jobs </A><BR><ACLASS="sect1"HREF="ch12_03.htm"TITLE="12.3 The &#34;Current Job&quot; Isn't Always What You Expect">The &quot;Current Job&quot; Isn't Always What You Expect</A><BR><ACLASS="sect1"HREF="ch12_04.htm"TITLE="12.4 Job Control and autowrite: Real Time Savers! ">Job Control and autowrite: Real Time Savers! </A><BR><ACLASS="sect1"HREF="ch12_05.htm"TITLE="12.5 System Overloaded? Try Stopping Some Jobs ">System Overloaded? Try Stopping Some Jobs </A><BR><ACLASS="sect1"HREF="ch12_06.htm"TITLE="12.6 Notification When Jobs Change State ">Notification When Jobs Change State </A><BR><ACLASS="sect1"HREF="ch12_07.htm"TITLE="12.7 Stop Background Output with stty tostop ">Stop Background Output with stty tostop </A><BR><ACLASS="sect1"HREF="ch12_08.htm"TITLE="12.8 Job Control in a Nutshell ">Job Control in a Nutshell </A><BR><ACLASS="sect1"HREF="ch12_09.htm"TITLE="12.9 Running Multiple Shell Sessions with screen ">Running Multiple Shell Sessions with screen </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0439">12.1 Job Control: Work Faster, Stop Runaway Jobs </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13440"></A><ACLASS="indexterm"NAME="AUTOID-13443"></A>Multitasking, letting you run more than one program at a time, is one ofthe great things about UNIX.Before job control, though, you had to decide ahead of time whether youwanted to run a job in the foreground (on your screen) or in thebackground (where you couldn't touch the program except to terminate itbefore it finished).</P><PCLASS="para">The C shell&nbsp;- and other shells since it, including some new Bourneshells&nbsp;- have job control built into them.You can start and stop jobs, pop them in and out of the background,and more.Windowing systems, which let you have multiple terminal windowsactive on the screen at the same time, make this less essential.Still, there are some important areas where you'll get more productivityout of job control than from simply opening another window.This article is an introduction to job control&nbsp;- there's more to learn.</P><PCLASS="para">Job control takes more than a shell to work right: the UNIX kernel hasto support it.Berkeley UNIX since BSD 4.0 has had job control, so most Berkeley-type UNIXeswill have it, too.Most versions of UNIX System V before Release 4 did not have job control.If your UNIX doesn't support job control, you can still put a job in thebackground&nbsp;- see the last paragraph in this article.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-439-SECT-1.1">12.1.1 Foreground and Background </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13450"></A><ACLASS="indexterm"NAME="AUTOID-13452"></A>UNIX distinguishes between foreground and backgroundprograms. This feature allows you to run several programssimultaneously from your terminal.When a program is running in the foreground, anything youtype at the keyboard is sent to the program's standard input unlessyou have redirected it. As a result, you can't do anything else untilthe program finishes. When you run a programin the background, it is disconnected from the keyboard.Anything you type reaches the UNIX shell and is interpreted as acommand. Therefore, you can run many programs simultaneously in thebackground. You can run only one program at a time in theforeground.</P><PCLASS="para">To run a program in the background, type an ampersand (<CODECLASS="literal">&amp;</CODE>)at the end of the command line. For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>f77 program.F &amp;</B></CODE>[1] 9145%</PRE></BLOCKQUOTE></P><PCLASS="para">This runs a FORTRAN compilation in the background, letting youcontinue other work while the compilation proceeds.  The shellresponds by printing a job number in brackets (<CODECLASS="literal">[]</CODE>), followedby the<SPANCLASS="link">process identification (PID) number (<ACLASS="linkend"HREF="ch38_03.htm"TITLE="Managing Processes: Overall Concepts ">38.3</A>)</SPAN>for the command.It then prompts you for a new command. Entering the command <EMCLASS="emphasis">jobs</EM> produces a short report describing all the programs you are executing in the background. For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>f77 program.F &amp;</B></CODE>[1] 9145% <CODECLASS="userinput"><B>jobs</B></CODE>[1]     + Running       f77 program.F%</PRE></BLOCKQUOTE></P><PCLASS="para">To bring a program from the background into the foreground, use the<ACLASS="indexterm"NAME="AUTOID-13468"></A>foreground command, <EMCLASS="emphasis">fg</EM>. If you have more than one background job, follow <EMCLASS="emphasis">fg</EM> with a job identifier&nbsp;- a percent sign(<CODECLASS="literal">%</CODE>) followed by the job number: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>jobs</B></CODE>[1]     - Running       f77 program.F[2]     + Stopped       vi sinus.F% <CODECLASS="userinput"><B>fg %1</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13478"></A>The plus sign (<CODECLASS="literal">+</CODE>) in the report from <EMCLASS="emphasis">jobs</EM> indicates which job will return to the foreground<SPANCLASS="link">by default (<ACLASS="linkend"HREF="ch12_03.htm"TITLE="The &#34;Current Job&quot; Isn't Always What You Expect">12.3</A>)</SPAN>.</P><PCLASS="para">To suspend a job running in the foreground, press<ACLASS="indexterm"NAME="AUTOID-13485"></A>CTRL-z.[You can use this to stop most frozen or runaway programs until youfigure out what to do next.Also, CTRL-z can stop programs that<SPANCLASS="link">interrupt characters (<ACLASS="linkend"HREF="ch05_09.htm"TITLE="Setting Your Erase, Kill, and Interrupt Characters ">5.9</A>)</SPAN>likeCTRL-c can't.<EMCLASS="emphasis">-JP</EM>&nbsp;]</P><PCLASS="para">Entering the <ACLASS="indexterm"NAME="AUTOID-13490"></A>background command, <EMCLASS="emphasis">bg</EM>, lets a stopped program continue execution in the background. The foreground command, <EMCLASS="emphasis">fg</EM>, restores thisprogram to execution in the foreground. For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>f77 -o program program.F</B></CODE><KBDCLASS="keycap">[CTRL-z]</KBD>Stopped% <CODECLASS="userinput"><B>bg</B></CODE>[1]     + Running       f77 -o program program.F%</PRE></BLOCKQUOTE></P><PCLASS="para">There is no prompt after the <EMCLASS="emphasis">f77</EM> command because the compileris running in the foreground. After you pressCTRL-z,the shell prints the word &quot;Stopped&quot; to indicate that it has stoppedexecution. At this point, you can enter any command; the <EMCLASS="emphasis">bg</EM>command lets the job continue executing in the background. Thisfeature is useful if you forget to type an <CODECLASS="literal">&amp;</CODE> at the end of thecommand line or if you decide to do something else while the job isrunning.</P><PCLASS="para">To terminate a background job, you can use the command's job numberrather than its PID number, as follows:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>kill %1</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13508"></A>If you omit it, UNIX interprets the job number as a process number.This will probably be the process number of some operating system function. UNIX will not let you make such a mistake unless you are<SPANCLASS="link">superuser (<ACLASS="linkend"HREF="ch01_24.htm"TITLE="The Superuser (Root) ">1.24</A>)</SPAN>.If you are superuser, the command is fatal.You may be superuser from time to time and therefore should not developsloppy habits.</P><PCLASS="para">In the next few seconds, press RETURNa few times.You should see the message: </P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">[1]  Terminated    f77 -o program program.F</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-13516"></A>If you don't see that, use the <EMCLASS="emphasis">jobs</EM> command to checkwhether the job is still running. If it's still running,use the <EMCLASS="emphasis">-9</EM> option as a last resort:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>kill -9 %1</B></CODE>[1]  Killed        f77 -o program program.F</PRE></BLOCKQUOTE></P><PCLASS="para">The <EMCLASS="emphasis">-9</EM> option doesn't give the process a chance to clean up itstemporary files and exit gracefully, so don't use it unless you need to.</P><PCLASS="para">A program running in the background cannot read input from a terminal. If a background job needs terminal input, it will stop; the <EMCLASS="emphasis">jobs</EM> command will print the message <CODECLASS="literal">Stopped (tty input)</CODE>.Before the programcan continue, you must bring it into the foreground with the <EMCLASS="emphasis">fg</EM>command and type the required input. You can save yourself thistrouble by redirecting the program's input so that itreads all its data from a file. You may alsowant to redirect standard output and standard error.If you don't, any output the program produces will appear on your terminal(unless you've used<SPANCLASS="link"><EMCLASS="emphasis">stty tostop</EM> (<ACLASS="linkend"HREF="ch12_07.htm"TITLE="Stop Background Output with stty tostop ">12.7</A>)</SPAN>).Since you will probably be using other commands, having miscellaneousdata and other messages flying across your terminal may be confusing.</P><PCLASS="para">On systems and shells without job control features, an <CODECLASS="literal">&amp;</CODE>will start a command in the background. It isimpossible to move a job from the foreground to the backgroundor vice versa.The<SPANCLASS="link"><EMCLASS="emphasis">ps</EM> (<ACLASS="linkend"HREF="ch38_05.htm"TITLE="The ps Command ">38.5</A>)</SPAN>command is the only tool availablefor determining what background jobs you have running.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN> <SPANCLASS="bibliomisc">from O'Reilly &amp; Associates' <CITECLASS="citetitle">UNIX for FORTRAN Programmers</CITE>, Chapter 1</SPAN></P></DIV></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch11_16.htm"TITLE="11.16 Instead of Changing History Characters "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 11.16 Instead of Changing History Characters "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch12_02.htm"TITLE="12.2 Other Ways to Refer to Jobs "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 12.2 Other Ways to Refer to Jobs "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">11.16 Instead of Changing History Characters </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">12.2 Other Ways to Refer to Jobs </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -