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

📄 ch38_11.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 38] 38.11 Printer Queue Watcher: A Restartable Daemon Shell Script</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:49:47Z"><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="ch38_01.htm"TITLE="38. Starting, Stopping, and Killing Processes"><LINKREL="prev"HREF="ch38_10.htm"TITLE="38.10 Destroying Processes with kill "><LINKREL="next"HREF="ch38_12.htm"TITLE="38.12 Killing All Your Processes "></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="ch38_10.htm"TITLE="38.10 Destroying Processes with kill "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.10 Destroying Processes with kill "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 38<BR>Starting, Stopping, and Killing Processes</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch38_12.htm"TITLE="38.12 Killing All Your Processes "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.12 Killing All Your Processes "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0212">38.11 Printer Queue Watcher: A Restartable Daemon Shell Script</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43076"></A>[This article may not appear to have a lot to do with the subject of thischapter, but it illustrates the other side of signal handling&nbsp;- what aprogram or shell script can do when it receives a signal.Jerry's script uses the <SPANCLASS="link"><EMCLASS="emphasis">trap</EM> (<ACLASS="linkend"HREF="ch44_12.htm"TITLE="Trapping Exits Caused by Interrupts ">44.12</A>)</SPAN>command to catch several different signals, and act differentlydepending on whether the signal is a &quot;hangup&quot; (<ICLASS="filename">HUP</I>, or signal 1)or a <ICLASS="filename">TERM</I> (signal 15). -TOR&nbsp;]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43084"></A>UNIX systems run &quot;daemon&quot; programs like <EMCLASS="emphasis">cron</EM>(8) and <EMCLASS="emphasis">syslogd</EM>(8)that wait in the background, looking for work to do.Many daemons read configuration files when they start up.System administrators sometimes change the configuration files and wantthe daemon to re-read the file.One way to do that is by terminating and restarting the program&nbsp;- butthat's ugly and also means the daemon won't be running for a few secondsuntil it's restarted.So, many daemons are designed to re-read their configuration filesand/or restart themselves when they get a signal (usually, the HUPsignal, signal 1).System administrators do this by getting the daemon's process ID numberand sending the signal with the <EMCLASS="emphasis">kill</EM> command.Because the daemon &quot;catches&quot; the signal, the daemon isn't actually killed.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43091"></A><ACLASS="indexterm"NAME="AUTOID-43094"></A><ACLASS="indexterm"NAME="AUTOID-43096"></A>You can run a shell script as a daemon by putting it in thebackground.[4]Here's a simple example, a shell script named <EMCLASS="emphasis">watchq</EM>.It reads a file full of printer queue names and stores it in a shell variable.Every 30 seconds, it runs<SPANCLASS="link"><EMCLASS="emphasis">lpq</EM> (<ACLASS="linkend"HREF="ch43_02.htm"TITLE="Introduction to Printing on UNIX ">43.2</A>)</SPAN>on all printer queues listed.If any queues have an error, the script echoes a message and the outputof <EMCLASS="emphasis">lpq</EM> to a particular user with the<SPANCLASS="link"><EMCLASS="emphasis">write</EM> (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>command.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[4] It's usually also a good idea to be sure that<SPANCLASS="link">the input and outputs are redirected (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>, <ACLASS="linkend"HREF="ch45_21.htm"TITLE="n&gt;&amp;m: Swap Standard Output and Standard Error ">45.21</A>)</SPAN>away from the terminal, maybe to the system console instead.On systems and shells that kill background jobs when you log out, use<SPANCLASS="link"><EMCLASS="emphasis">nohup</EM> (<ACLASS="linkend"HREF="ch38_18.htm"TITLE="nohup ">38.18</A>)</SPAN>.</P></BLOCKQUOTE><PCLASS="para">After the script has run for a while, the printer named <EMCLASS="emphasis">office</EM> goes down.I edit the <EMCLASS="emphasis">watchqs</EM> file and remove that printer so the poor user<EMCLASS="emphasis">lisa</EM> won't keep getting complaints about it.Then I send a signal to have the file re-read:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch13_14.htm"TITLE="13.14 What Can You Do with an Empty File? ">/dev/null</A> <ACLASS="co"HREF="ch13_13.htm"TITLE='13.13 The "Filename&quot; - '>-</A> <ACLASS="co"HREF="ch38_10.htm"TITLE="38.10 Destroying Processes with kill ">kill</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">&#13;% <CODECLASS="userinput"><B>cat watchq</B></CODE>#! /bin/sh# watchq - &quot;daemon&quot; script that watches printer queue(s) for errorstemp=/tmp/WATCHQ$$             # Holds output of lpqwatch=/usr/local/lib/watchqs   # Queue names to watchwriteto=lisa                   # User who gets notices about printerqueues=&quot;`cat $watch`&quot;          # Put list of queue names in $queuestrap 'queues=&quot;`cat $watch`&quot;' 1 # Reset $queues if we get a SIGHUPtrap 'rm -f $temp; exit' 0 15  # Clean up temp file when killed# Loop forever (until someone kills script):while :do    for queue in $queues    do        lpq -P$queue &gt;$temp        if egrep '(out of paper|error|warning)' $temp &gt;/dev/null        then echo &quot;PRINTER QUEUE $queue:&quot; | cat - $temp | write $writeto        fi    done    sleep 30done% <CODECLASS="userinput"><B>echo office main lobby &gt; /usr/local/lib/watchqs</B></CODE>% <CODECLASS="userinput"><B>watchq &amp;</B></CODE>[1] 4363   ...% <CODECLASS="userinput"><B>echo main lobby &gt; /usr/local/lib/watchqs</B></CODE>%<CODECLASS="userinput"><B> kill -1 4363</B></CODE>   ...% <CODECLASS="userinput"><B>kill 4363</B></CODE>[1]    Exit -48             watchq</PRE></TD></TR></TABLE></P><PCLASS="para">In real life, the <EMCLASS="emphasis">watchq</EM> script might be started from a system filelike <EMCLASS="emphasis">/etc/rc.local</EM> when the system reboots.Lisa would probably edit the <EMCLASS="emphasis">watchqs</EM> file herself.The username that's notified by <EMCLASS="emphasis">write</EM> might also be resettable with a<EMCLASS="emphasis">kill -1</EM>.</P><PCLASS="para">This isn't foolproof and you can run into subtle problems.For instance, the <EMCLASS="emphasis">write</EM> command may not work on some UNIXes ifit's running from a daemon without a<SPANCLASS="link">controlling tty (<ACLASS="linkend"HREF="ch38_06.htm"TITLE="The Controlling Terminal ">38.6</A>)</SPAN>.Also, the error messages that<SPANCLASS="link"><EMCLASS="emphasis">egrep</EM> (<ACLASS="linkend"HREF="ch27_05.htm"TITLE="Extended Searching for Text with egrep ">27.5</A>)</SPAN>searches for may not catch allproblems and are system-dependent.But this script is just a demonstration&nbsp;- to show a great way to write aquick-and-dirty daemon.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></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="ch38_10.htm"TITLE="38.10 Destroying Processes with kill "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 38.10 Destroying Processes with kill "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="ch38_12.htm"TITLE="38.12 Killing All Your Processes "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 38.12 Killing All Your Processes "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">38.10 Destroying Processes with kill </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">38.12 Killing All Your Processes </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 + -