<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Saturnboy &#187; daemon</title>
	<atom:link href="http://saturnboy.com/tag/daemon/feed/" rel="self" type="application/rss+xml" />
	<link>http://saturnboy.com</link>
	<description>Code, Work, and Life</description>
	<lastBuildDate>Wed, 28 Jul 2010 13:20:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Nginx Init Script</title>
		<link>http://saturnboy.com/2008/10/nginx-init-script/</link>
		<comments>http://saturnboy.com/2008/10/nginx-init-script/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 20:08:09 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[daemon]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[slicehost]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=31</guid>
		<description><![CDATA[This blog is running WordPress on Nginx on a 256 slice from Slicehost.  I&#8217;ve diligently followed all of the awesome articles by Slicehost&#8217;s tutorial master, Pickled Onion.  And I even googled some other great stuff.  But when it came down to PHP, spawn-fcgi, and particularly the Ubuntu way of daemon initialization, I had to blaze [...]]]></description>
			<content:encoded><![CDATA[<p>This blog is running <a href="http://wordpress.org/">WordPress</a> on <a href="http://nginx.net/">Nginx</a> on a 256 slice from <a href="http://www.slicehost.com/">Slicehost</a>.  I&#8217;ve diligently followed all of the <a href="http://articles.slicehost.com/">awesome articles</a> by Slicehost&#8217;s tutorial master, Pickled Onion.  And I even googled some other <a href="http://codeintensity.blogspot.com/2008/08/wordpress-nginx-subdirectories-and.html">great stuff</a>.  But when it came down to PHP, spawn-fcgi, and particularly the Ubuntu way of daemon initialization, I had to blaze a new trail.</p>
<p>Here is the <code>spawn-fcgi-php</code> script that I came up with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/sh</span>
&nbsp;
<span style="color: #666666; font-style: italic;">### BEGIN INIT INFO</span>
<span style="color: #666666; font-style: italic;"># Provides:          spawn-fcgi-php</span>
<span style="color: #666666; font-style: italic;"># Required-Start:    $all</span>
<span style="color: #666666; font-style: italic;"># Required-Stop:     $all</span>
<span style="color: #666666; font-style: italic;"># Default-Start:     2 3 4 5</span>
<span style="color: #666666; font-style: italic;"># Default-Stop:      0 1 6</span>
<span style="color: #666666; font-style: italic;"># Short-Description: starts FastCGI for PHP</span>
<span style="color: #666666; font-style: italic;"># Description:       starts FastCGI for PHP using start-stop-daemon</span>
<span style="color: #666666; font-style: italic;">### END INIT INFO</span>
&nbsp;
<span style="color: #007800;">PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin
<span style="color: #007800;">NAME</span>=spawn-fcgi-php
<span style="color: #007800;">PID</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>spawn-fcgi-php.pid
<span style="color: #007800;">DAEMON</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>spawn-fcgi
<span style="color: #007800;">DAEMON_OPTS</span>=<span style="color: #ff0000;">&quot;-f /usr/bin/php-cgi -a 127.0.0.1 -p 55155 -u www-data -g www-data -P <span style="color: #007800;">$PID</span>&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-e</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
  start<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Starting <span style="color: #007800;">$NAME</span>: &quot;</span>
        start-stop-daemon <span style="color: #660033;">--start</span> <span style="color: #660033;">--pidfile</span> <span style="color: #007800;">$PID</span> <span style="color: #660033;">--exec</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #660033;">--</span> <span style="color: #007800;">$DAEMON_OPTS</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done.&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
  stop<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Stopping <span style="color: #007800;">$NAME</span>: &quot;</span>
        start-stop-daemon <span style="color: #660033;">--stop</span>  <span style="color: #660033;">--pidfile</span> <span style="color: #007800;">$PID</span> <span style="color: #660033;">--retry</span> <span style="color: #000000;">5</span>
        <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$PID</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done.&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
  restart<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Stopping <span style="color: #007800;">$NAME</span>: &quot;</span>
        start-stop-daemon <span style="color: #660033;">--stop</span>  <span style="color: #660033;">--pidfile</span> <span style="color: #007800;">$PID</span> <span style="color: #660033;">--retry</span> <span style="color: #000000;">5</span>
        <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$PID</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done...&quot;</span>
        <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Starting <span style="color: #007800;">$NAME</span>: &quot;</span>
        start-stop-daemon <span style="color: #660033;">--start</span> <span style="color: #660033;">--pidfile</span> <span style="color: #007800;">$PID</span> <span style="color: #660033;">--exec</span> <span style="color: #007800;">$DAEMON</span> <span style="color: #660033;">--</span> <span style="color: #007800;">$DAEMON_OPTS</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done.&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: /etc/init.d/<span style="color: #007800;">$NAME</span> {start|stop|restart}&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></div></div>

<p>A pretty straight forward script the does just two things: <code>start</code> and <code>stop</code>.  The <code>restart</code> is just a convenience option, composed of a <code>stop</code> plus a <code>start</code> with a <code>sleep 1</code> in between.</p>
<h4>start</h4>
<p>The script starts <code>spawn-fcgi</code> from <a href="http://www.lighttpd.net/">lightty</a> with a couple of options, most importantly a port and a pid-file.  By default, <code>spawn-fcgi</code> will start up 5 child instances of <code>php-cgi</code>.  So a quick <code>ps aux | grep cgi</code> will show 6 processes.  From the limited (aka non-existent) performance data of my blog, this seems like a fine number.</p>
<h4>stop</h4>
<p>The script stops all those instances by sending a SIGTERM (-15) to the parent process recorded in the pid-file.  Stopping with the <code>--retry 5</code> option, will first send the default SIGTERM signal and check for 5 seconds if the process is still running, then send a KILL (-9) signal and wait another 5 seconds before erroring out.</p>
<blockquote class="deeper"><p><b>Digging Deeper:</b> When stopping, pass <b>only one</b> matching option to the <code>start-stop-daemon</code>.  In most cases, it&#8217;s easiest to just pass the pid-file via the <code>--pidfile $PID</code> option (you might find some scripts online that pass <code>--exec $DAEMON</code> as a second matching option, but this didn&#8217;t work for me).</p></blockquote>
<p>Just copy this script into <code>/etc/init.d/</code>, given it <code>755</code> permissions, and add it to the init sequence with <code>sudo update-rc.d spawn-fcgi-php defaults</code>.  After a quick reboot, you should see 6 <code>php-cgi</code> processes running.</p>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2008/10/nginx-init-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
