<?xml version="1.0"?><!-- generator="bbPress" -->

<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>

<channel>
<title>DIYMacServer &#187; Topic: using launchd instead of startupitems</title>
<link>http://diymacserver.com/forum/</link>
<description>DIYMacServer &#187; Topic: using launchd instead of startupitems</description>
<language>en</language>
<pubDate>Wed, 08 Feb 2012 18:56:37 +0000</pubDate>

<item>
<title>dtgriscom on "using launchd instead of startupitems"</title>
<link>http://diymacserver.com/forum/topic/using-launchd-instead-of-startupitems#post-240</link>
<pubDate>Tue, 05 Dec 2006 13:05:08 +0000</pubDate>
<dc:creator>dtgriscom</dc:creator>
<guid isPermaLink="false">240@http://diymacserver.com/forum/</guid>
<description>I've used this to get Apache starting on boot, which was very helpful. I'd like to share what I learned in the process:&#60;br /&#62;
&#60;br /&#62;
1: There's a typo in the launchd code. The following line&#60;br /&#62;
&#60;br /&#62;
&#60;p&#62;&#60;code&#62;&#38;lt;string&#38;gt;/Library/Apache2/bin/launchd_apache.shl&#38;lt;/string&#38;gt;&#60;/code&#62;&#60;/p&#62;&#60;br /&#62;
&#60;br /&#62;
should be&#60;br /&#62;
&#60;br /&#62;
&#60;p&#62;&#60;code&#62;&#38;lt;string&#38;gt;/Library/Apache2/bin/launchd_apache.sh&#38;lt;/string&#38;gt;&#60;/code&#62;&#60;/p&#62;&#60;br /&#62;
&#60;br /&#62;
2: The launchd_apache.sh assumes that the Apache user is www; on my system it's root. To change this, change both occurrences of&#60;br /&#62;
&#60;br /&#62;
&#60;p&#62;&#60;code&#62;httpdArray=&#38;#40;`ps -U www &#124; grep httpd &#124; awk '&#38;#123;print $1&#38;#125;'`&#38;#41;; &#60;/code&#62;&#60;/p&#62; &#60;br /&#62;
&#60;br /&#62;
to &#60;br /&#62;
&#60;br /&#62;
&#60;p&#62;&#60;code&#62;httpdArray=&#38;#40;`ps -U root &#124; grep httpd &#124; awk '&#38;#123;print $1&#38;#125;'`&#38;#41;; &#60;/code&#62;&#60;/p&#62;&#60;br /&#62;
&#60;br /&#62;
3: A more complete description of launchd_apache.sh would be helpful to random Googlers (such as myself). launchd launches processes, and expects those processes to continue forever; it treats a process that ends as an error, and relaunches it. apachectl normally starts httpd and then exits, which would confuse launchd. So, here launchd runs launchd_apache.sh. launchd_apache.sh runs apachectl, then sits there watching the process list for the resulting httpd instances (matched by user and &#38;quot;httpd&#38;quot;). As long as there are such processes, launchd_apache.sh continues waiting; once they are all gone, launchd_apache.sh exits, and launchd restarts launchd_apache.sh.&#60;br /&#62;
&#60;br /&#62;
4: If you elsewhere run &#38;quot;apachectl graceful&#38;quot; to restart httpd, then it's possible launchd_apache.sh will see that moment when there are no httpd instances, exit, and cause launchd to run its own &#38;quot;apachectl graceful&#38;quot;. That means the clean way to do this would be to run &#38;quot;apachectl stop&#38;quot; and wait the 40 seconds for launchd to restart things. In reality, this is very unlikely, and isn't a big deal if it does happen, so &#38;quot;apachectl graceful&#38;quot; is still probably the way to go (and avoids your waiting for launchd to kick in).&#60;br /&#62;
&#60;br /&#62;
Thanks for the useful post,&#60;br /&#62;
Dan</description>
</item>
<item>
<title>Richard on "using launchd instead of startupitems"</title>
<link>http://diymacserver.com/forum/topic/using-launchd-instead-of-startupitems#post-211</link>
<pubDate>Wed, 15 Nov 2006 18:44:40 +0000</pubDate>
<dc:creator>Richard</dc:creator>
<guid isPermaLink="false">211@http://diymacserver.com/forum/</guid>
<description>Thanks Michael for posting this explanation. I will add my opinion here as well as I did via email to Michael. &#60;br /&#62;
&#60;br /&#62;
I like the solution Michael has presented and it does have it advantages, but it also has some drawbacks. &#60;br /&#62;
&#60;br /&#62;
Most of the software, like MySQL and Apache, came with startup scripts and it was the way postfix was normally started on OS X. I want to support the most simple and standard way that most people/systems use so I copied that and continued on that note. With the startupparameters.plist file you can define dependancies like what has to be running.   For instance DSPAM need MyQL running.&#60;br /&#62;
&#60;br /&#62;
Michael provides a different solution for the same task and that's great. It's all about choice for me and therefore urged MIchael to post his solution here for everybody's benefit.</description>
</item>
<item>
<title>Michael on "using launchd instead of startupitems"</title>
<link>http://diymacserver.com/forum/topic/using-launchd-instead-of-startupitems#post-187</link>
<pubDate>Sat, 11 Nov 2006 21:47:33 +0000</pubDate>
<dc:creator>Michael</dc:creator>
<guid isPermaLink="false">187@http://diymacserver.com/forum/</guid>
<description>I prefer launchd to startupItems.  There's not currently much advantage to one or the other the way I use launchd, except that if my database crashes launchd will restart it.&#60;br /&#62;
&#60;br /&#62;
Richard and I discussed this and thought it might be useful to bring to the forum, so here it is.&#60;br /&#62;
&#60;br /&#62;
I start MySQL with the following file as /Library/LaunchDaemons/org.mysql.MySQL5.plist:&#60;br /&#62;
&#60;p&#62;&#60;code&#62;&#38;nbsp;&#38;lt;?xml version=&#38;quot;1.0&#38;quot; encoding=&#38;quot;UTF-8&#38;quot;?&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;!DOCTYPE plist PUBLIC &#38;quot;-//Apple Computer//DTD PLIST 1.0//EN&#38;quot;&#60;br /&#62;
&#38;nbsp;&#38;quot;http&#38;#58;//www.apple.com/DTDs/PropertyList-1.0.dtd&#38;quot;&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;plist version=&#38;quot;1.0&#38;quot;&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;dict&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;GroupName&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;admin&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;Label&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;MySQL5&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;OnDemand&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;false/&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;Program&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;/usr/local/mysql/bin/mysqld_safe&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;RunAtLoad&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;true/&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;ServiceDescription&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;Launches the MySQL database server&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;UserName&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;root&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;/dict&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;/plist&#38;gt;&#60;/code&#62;&#60;/p&#62;&#60;br /&#62;
--&#60;br /&#62;
For Apache2, I have to use a shell script to get around the way launchd responds to apps that quit in less than 10 seconds.&#60;br /&#62;
The first file is launchd_apache.sh:&#60;br /&#62;
&#60;p&#62;&#60;code&#62;&#38;nbsp;#!/bin/sh&#60;br /&#62;
&#38;nbsp;/Library/Apache2/bin/apachectl graceful&#60;br /&#62;
&#38;nbsp;&#60;br /&#62;
&#38;nbsp;sleeptime=40&#60;br /&#62;
&#38;nbsp;&#60;br /&#62;
&#38;nbsp;httpdArray=&#38;#40;`ps -U www &#124; grep httpd &#124; awk '&#38;#123;print $1&#38;#125;'`&#38;#41;;&#60;br /&#62;
&#38;nbsp;let httpdCount=$&#38;#123;#httpdArray&#38;#91;*&#38;#93;&#38;#125;;&#60;br /&#62;
&#38;nbsp;&#60;br /&#62;
&#38;nbsp;while &#38;#40;&#38;#40;$httpdCount &#38;gt; 0&#38;#41;&#38;#41;&#60;br /&#62;
&#38;nbsp;do&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;sleep $sleeptime;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;hpptdArray=&#38;#40;`ps -U www &#124; grep httpd &#124; awk '&#38;#123;print $1&#38;#125;'`&#38;#41;;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;let httpdCount=$&#38;#123;#httpdArray&#38;#91;*&#38;#93;&#38;#125;;&#60;br /&#62;
&#38;nbsp;done&#60;br /&#62;
&#38;nbsp;&#60;br /&#62;
&#38;nbsp;echo &#38;quot;Apache Stopped, restarting...&#38;quot;&#60;/code&#62;&#60;/p&#62;&#60;br /&#62;
&#60;br /&#62;
The second is the LaunchDaemon that uses it:&#60;br /&#62;
&#60;p&#62;&#60;code&#62;&#38;nbsp;&#38;lt;?xml version=&#38;quot;1.0&#38;quot; encoding=&#38;quot;UTF-8&#38;quot;?&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;!DOCTYPE plist PUBLIC &#38;quot;-//Apple Computer//DTD PLIST 1.0//EN&#38;quot;&#60;br /&#62;
&#38;nbsp;&#38;quot;http&#38;#58;//www.apple.com/DTDs/PropertyList-1.0.dtd&#38;quot;&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;plist version=&#38;quot;1.0&#38;quot;&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;dict&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;GroupName&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;admin&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;Label&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;Apache2&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;OnDemand&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;false/&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;Program&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;/Library/Apache2/bin/launchd_apache.sh&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;ProgramArguments&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;array&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;/Library/Apache2/bin&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;graceful&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;/array&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;RunAtLoad&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;true/&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;ServiceDescription&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;Launches the Apache2 httpd web server&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;key&#38;gt;UserName&#38;lt;/key&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;nbsp; &#38;nbsp;&#38;lt;string&#38;gt;root&#38;lt;/string&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;/dict&#38;gt;&#60;br /&#62;
&#38;nbsp;&#38;lt;/plist&#38;gt;&#60;/code&#62;&#60;/p&#62;&#60;br /&#62;
&#60;br /&#62;
Edit: fixed typo.  Thanks, dtgriscom!</description>
</item>

</channel>
</rss>

