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

📄 ch17_17.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 17.16. Restarting a Server on Demand (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:44:45Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-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="ch17_01.htm"TITLE="17. Sockets"><LINKREL="prev"HREF="ch17_16.htm"TITLE="17.15. Making a Daemon Server"><LINKREL="next"HREF="ch17_18.htm"TITLE="17.17. Program: backsniff"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_16.htm"TITLE="17.15. Making a Daemon Server"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.15. Making a Daemon Server"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch17_01.htm"TITLE="17. Sockets"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_18.htm"TITLE="17.17. Program: backsniff"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.17. Program: backsniff"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch17-33998">17.16. Restarting a Server on Demand</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-2026">Problem<ACLASS="indexterm"NAME="ch17-idx-1000004877-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004877-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004877-2"></A><ACLASS="indexterm"NAME="ch17-idx-1000004877-3"></A></A></H3><PCLASS="para">You want your server to shutdown and restart when it receives a HUP signal, just like <CODECLASS="literal">inetd</CODE> or <CODECLASS="literal">httpd </CODE>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-2032">Solution</A></H3><PCLASS="para">Catch the <CODECLASS="literal">SIGHUP</CODE> signal, and re-execute your program:</P><PRECLASS="programlisting">$SELF = &quot;/usr/local/libexec/myd&quot;;   # which program I am@ARGS = qw(-l /var/log/myd -d);     # program arguments$SIG{HUP} = \&amp;phoenix;sub phoenix {    # close all your connections, kill your children, and    # generally prepare to be reincarnated with dignity.    exec($SELF, @ARGS)              or die &quot;Couldn't restart: $!\n&quot;;}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-2058">Discussion</A></H3><PCLASS="para">It sure looks simple ("when I get a HUP signal, restart") but it's tricky. You must know your own program name, and that isn't easy to find out. You could use <CODECLASS="literal">$0</CODE> or the FindBin module. For normal programs, this is fine, but critical system utilities must be more cautious, as there's no guarantee that <CODECLASS="literal">$0</CODE> is valid. You can hardcode the filename and arguments into your program, as we do here. That's not necessarily the most convenient solution, however, so you might want to read the program and arguments from an external file (using the filesystem's protections to ensure it hasn't been tampered with).</P><PCLASS="para">Be sure and install your signal handler <EMCLASS="emphasis">after</EM> you define <CODECLASS="literal">$SELF</CODE> and <CODECLASS="literal">@ARGS</CODE>, otherwise there's a race condition when a SIGHUP could run <CODECLASS="literal">restart</CODE> but you don't know the program to run. This would cause your program to die.</P><PCLASS="para">Some servers don't want restart on receiving a SIGHUP&nbsp;- they just want to reread their configuration file.</P><PRECLASS="programlisting">$CONFIG_FILE = &quot;/usr/local/etc/myprog/server_conf.pl&quot;;$SIG{HUP} = \&amp;read_config;sub read_config {    do $CONFIG_FILE;} </PRE><PCLASS="para">Some clever servers even autoload their configuration files when they notice that those files have been updated. That way you don't have to go out of your way to signal them.<ACLASS="indexterm"NAME="ch17-idx-1000004879-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004879-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004879-2"></A><ACLASS="indexterm"NAME="ch17-idx-1000004879-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-2080">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch03_033.htm">&#13;<CODECLASS="literal">exec</CODE></A> function in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlfunc </I>(1); <ACLASS="xref"HREF="ch08_17.htm"TITLE="Reading Configuration Files">Recipe 8.16</A>; <ACLASS="xref"HREF="ch08_18.htm"TITLE="Testing a File for Trustworthiness">Recipe 8.17</A>; <ACLASS="xref"HREF="ch16_16.htm"TITLE="Installing a Signal Handler">Recipe 16.15</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_16.htm"TITLE="17.15. Making a Daemon Server"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.15. Making a Daemon Server"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_18.htm"TITLE="17.17. Program: backsniff"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.17. Program: backsniff"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">17.15. Making a Daemon Server</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">17.17. Program: backsniff</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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