


<?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>CantRemembrances &#187; logrotate</title>
	<atom:link href="http://blog.cantremember.com/tag/logrotate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cantremember.com</link>
	<description>Memes of a technical vein discovered during CantRemember.com implementation</description>
	<lastBuildDate>Tue, 16 Feb 2010 06:36:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>logrotate Mac OS Launch Daemon with Legacy MacPort</title>
		<link>http://blog.cantremember.com/logrotate-mac-os-launch-daemon-with-legacy-macport/</link>
		<comments>http://blog.cantremember.com/logrotate-mac-os-launch-daemon-with-legacy-macport/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 07:04:06 +0000</pubDate>
		<dc:creator>dfoley</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[logrotate]]></category>
		<category><![CDATA[macports]]></category>
		<category><![CDATA[osx]]></category>

		<guid isPermaLink="false">http://blog.cantremember.com/?p=37</guid>
		<description><![CDATA[Everbody loves cron.  The classic basic scheduler, 80/20 flexibility, gets the job done.  So, when I started with OS X, I went looking for cron.
Yes, you can cron if you want to.  Or maybe, as the Mac children recommend, you can create Launch Daemons.  It&#8217;s simply a custom launchd.plist, an XML [...]]]></description>
			<content:encoded><![CDATA[<p>Everbody loves <a href="http://www.manpagez.com/man/8/cron/">cron</a>.  The classic basic scheduler, 80/20 flexibility, gets the job done.  So, when I started with OS X, I went looking for cron.</p>
<p>Yes, you can <em>cron</em> if you want to.  Or maybe, as the Mac children recommend, you can create <strong>Launch Daemons</strong>.  It&#8217;s simply a custom <a href="http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/ManPages/man5/launchd.plist.5.html">launchd.plist</a>, an XML file to define tasks in Apple&#8217;s terms.  Sweet, I can do that&#8230;</p>
<h3>The logrotated.plist Daemon</h3>
<p>From what I could tell, out of the box, Leopard doesn&#8217;t give you <a href="http://linux.die.net/man/8/logrotate">logrotate</a>.  So, here it is as a Launch Daemon:</p>
<div class="pre_wrap">
<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
	&lt;key&gt;Disabled&lt;/key&gt;
	&lt;false/&gt;
	&lt;key&gt;Label&lt;/key&gt;
	&lt;string&gt;logrotate&lt;/string&gt;

	&lt;key&gt;ProgramArguments&lt;/key&gt;
	&lt;array&gt;
		&lt;string&gt;/opt/local/sbin/logrotate&lt;/string&gt;
		&lt;string&gt;-f&lt;/string&gt;
		&lt;string&gt;/etc/logrotate/*.conf&lt;/string&gt;
	&lt;/array&gt;
	&lt;key&gt;EnableGlobbing&lt;/key&gt;
	&lt;true/&gt;

	&lt;key&gt;KeepAlive&lt;/key&gt;
	&lt;false/&gt;
	&lt;key&gt;LaunchOnlyOnce&lt;/key&gt;
	&lt;false/&gt;
	&lt;key&gt;RunAtLoad&lt;/key&gt;
	&lt;false/&gt;
&lt;!--
causes 'Throttling respawn: Will start in 10 seconds' in system.log
	&lt;key&gt;OnDemand&lt;/key&gt;
	&lt;false/&gt;
--&gt;

&lt;!--
http://www.nabble.com/launchd-support-ranges-for-StartCalendarInterval - - td14857719.html
--&gt;
	&lt;key&gt;StartCalendarInterval&lt;/key&gt;
	&lt;!-- 3:05a, 3:05p --&gt;
	&lt;array&gt;
		&lt;dict&gt;
			&lt;key&gt;Hour&lt;/key&gt;
			&lt;integer&gt;3&lt;/integer&gt;
			&lt;key&gt;Minute&lt;/key&gt;
			&lt;integer&gt;05&lt;/integer&gt;
		&lt;/dict&gt;
		&lt;dict&gt;
			&lt;key&gt;Hour&lt;/key&gt;
			&lt;integer&gt;15&lt;/integer&gt;
			&lt;key&gt;Minute&lt;/key&gt;
			&lt;integer&gt;05&lt;/integer&gt;
		&lt;/dict&gt;
	&lt;/array&gt;
&lt;/dict&gt;
&lt;/plist&gt;</code></pre>
</div>
<p>Great.  So what do I do with it?  Name it anything &#8216;*.plist&#8217; &#8212; <code>logrotated.plist</code> perhaps &#8212; and put it here:</p>
<ul>
<li><code>/System/Library/LaunchDaemons</code> or</li>
<li><code>/Library/LaunchDaemons</code></li>
</ul>
<p>I tend to use System for any services I wan to launch on boot (<a href="http://linux.die.net/man/8/chkconfig">chkconfig</a>-ish), and anything that&#8217;s just a scheduled task is more pedestrian.</p>
<p>You register daemons through <a href="http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/launchctl.1.html">launchctl</a>.  When you change it, just:</p>
<div class="pre_wrap">
<pre><code>$ launchctl unload FILE.plist
$ launchctl load FILE.plist</code></pre>
</div>
<p>Sure, not a hard thing.  What I <em>did</em> want to point out were some of the things that I learned:</p>
<ul>
<li>
Provide each space-separated component of the command line as &lt;string /&gt; element in the ProgramArgument &lt;array /&gt;.  It&#8217;s the easiest way to go.
</li>
<li>
I tend to keep the <code>KeepAlive</code>, <code>LaunchOnlyOnce</code> and <code>RunAtLoad</code> all in sync for each daemon.  It seems to guarantee compatability.
</li>
<li>
I still haven&#8217;t gotten quite the hand of <code>OnDemand</code> (c&#8217;mon, I&#8217;m new to this).  When you are developing your daemons, keep a watch on <code>/var/log/system.log</code> &#8212; if you screw up, you&#8217;ll probably see it there in one fashion or another.
</li>
<li>
<code>StartCalendarInterval</code> is the equivalent of the cron schedule mask.  See the <a href="http://www.nabble.com/launchd-support-ranges-for-StartCalendarInterval">Nabble</a> post i commented above.  You can&#8217;t do ranges, but wildcards are do-able sorta by omission.  If you want mulltiple disparate schedules, as you see above, you need to pass in an &lt;array /&gt; of &lt;dict /&gt;s, not just a single &lt;dict /&gt; when you only need one mask.
</li>
<li>
Yes, datatypes matter!  I couldn&#8217;t figure out for the life of me why my daemon wouldn&#8217;t start, until I realized that my <code>Hour</code> and <code>Minute</code> values were configured as &lt;string /&gt;s &#8212; vs &lt;integer /&gt;s &#8212; because I&#8217;d been, well, you know, trying to do ranges and wildcards the cron-style way.  <code>launchctl</code> didn&#8217;t complain, it just didn&#8217;t &#8230; work.  So don&#8217;t do that.
</li>
<li>
Any daemon which has a schedule that fires off when the system is down will be automatically executed shortly after the system re-awakens.  I don&#8217;t know exactly how quickly, but I&#8217;ve seen it in action (though I&#8217;ve read some dispute about its efficacy in my searches).
</li>
</ul>
<h3>The Trials and Tribulations of MacPorts</h3>
<p>But of course this Epistle wouldn&#8217;t be any fun without a twist, right?  Well, lucky us, because it wasn&#8217;t <em>that easy</em>.</p>
<p>I&#8217;m using <a href="http://guide.macports.org/">MacPorts</a> as my <code>yum</code>-my package manager.  It&#8217;s great, and easy to set up, but it&#8217;s quite as stable as I&#8217;ve seen under Linux.  Or rather, I&#8217;ve seen a disproportionate number of issues in the times I&#8217;ve used it.  However, I&#8217;m eternally grateful because it saves me so much time &#8230; the successes far outweigh the problems.</p>
<p>When things do go bad, as they did when I built logrotate 3.7.7, you have to set up a custom Portfile and source repository so that you can effectively drive MacPorts to fetch and build your specific version.  logrotate 3.7.1_1 turned out to be much more stable.</p>
<p><a href="http://homepage.mac.com/simx/technonova/tips/using_experimental_macports_portfiles.html">Techonova</a> and <a href="http://journal.bitshaker.com/articles/2007/10/20/install-old-versions-of-ports-using-macports/">Joe Homs</a> go into very excellent and welcome detail in their posts on how to pull this off.  A summary of what you need to do is:</p>
<ul>
<li>
watch the build failure &#8212; debug with <code>port -d</code> &#8212; to identify where the dying source is located.  That&#8217;ll be PATH/TO/PROJECT
</li>
<li>
visit <code>http://trac.macosforge.org/projects<wbr>/macports/log/trunk/dports<wbr>/PATH/TO/PROJECT</code> and snag the Portfile.  That&#8217;s a MacPorts spec file, and you can tweak it to do your bidding.
</li>
<li>
create a source directory that you&#8217;ll keep around for a while (mine is <code>/Users/Shared/macports</code>).
</li>
<li>
register that directory &#8212; your local Portfile repository &#8212; with <code>/opt/local/etc/macports/sources.conf</code> (using <code>file://</code> protocol)
</li>
<li>
copy the Portfile into a PATH/TO/PROJECT sub-directory structure (eg. <code>sysutils/logrotate</code>).  Basically, the same path you&#8217;ll have snagged it from.
</li>
<li>
pull down a previous source revision and snapshot it locally.  That could come from SVN or git, tarball, whatever.  Start search from the <code>homepage</code> setting of the Portfile.
</li>
<li>
tweak the Portfile to &#8216;make sense&#8217; for the source you&#8217;ve pulled down.  If you based upon a close-enough version, it should be as easy as tweaking the version, etc. &#8212; no custom build tasks.  I&#8217;m glossing over when I say that it&#8217;s usually a matter of version-based naming conventions and MD5 checksums.
</li>
<li>
re-build the repository&#8217;s PortIndex (which you&#8217;ll need to do every time you make an addition / change)
</li>
</ul>
<p>Please feel free to consult the other two posts to fill in the details that I was so cavalier about.</p>
<h3>In Summary</h3>
<p>This was another one of those occasions when I was glad I kept track of what I was doing while I was in the moment. Pack your config files with comments, because they&#8217;re invaluable.  And drop READMEs around where you&#8217;re likely to find them &#8230; I filled in a lot of the extra details for this post from those breadcrumbs I left for myself, things I could have easily wasted 15-20m on re-discovering by braille :)</p>
<p>Peace.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cantremember.com/logrotate-mac-os-launch-daemon-with-legacy-macport/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
