Using launchd to update MacPorts I want to use launchd to update my MacPorts tree periodically (i.e. port selfupdate.

I have created /Library/LaunchDaemons/org.macports.selfupdate.plist: 

$ ls -l /Library/LaunchDaemons/org.macports.selfupdate.plist
-rw-r--r--  1 root  wheel  669  8 Nov 12:49 /Library/LaunchDaemons/org.macports.selfupdate.plist


It has the following contents: 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//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;Label&lt;/key&gt;
&lt;string&gt;org.macports.selfupdate&lt;/string&gt;
&lt;key&gt;ProgramArguments&lt;/key&gt;
&lt;array&gt;
    &lt;string&gt;/opt/local/bin/port&lt;/string&gt;
    &lt;string&gt;selfupdate&lt;/string&gt;
&lt;/array&gt;
&lt;key&gt;StandardOutPath&lt;/key&gt;
&lt;string&gt;/var/log/org.macports.selfupdate.log&lt;/string&gt;
&lt;key&gt;StandardErrorPath&lt;/key&gt;
&lt;string&gt;/var/log/org.macports.selfupdate.err&lt;/string&gt;
&lt;key&gt;RunAtLoad&lt;/key&gt;
&lt;true/&gt;
&lt;key&gt;StartInterval&lt;/key&gt;
&lt;integer&gt;604800&lt;/integer&gt;&lt;!-- 1 week --&gt;
&lt;/dict&gt;
&lt;/plist&gt;


I've loaded it and verified that it's loaded:

$ launchctl list | grep macports
-   78  org.macports.selfupdate


But it doesn't look like it actually run.  

$ port info wireshark
Warning: port definitions are more than two weeks old, consider updating them by running 'port selfupdate'.
wireshark @1.12.8_3 (net)
...


What am I missing?
You probably put the plist in ~/Library/LaunchAgents/. Since /opt/local is owned by root:wheel (that's at least the default), you don't have the proper permissions to write to certain necessary folders (e.g. /opt/local/var/macports/sources/rsync.macports.org/release/tarballs/tmp). The log files (i.e. /var/log/org.macports.selfupdate.log/err) neither can be created, because only root can write to /var/log/.

So run the selfupdate service as launch daemon:


Remove the launch agent from the launchd database:

launchctl remove org.macports.selfupdate

Move the plist to /Library/LaunchDaemons/:

sudo mv ~/Library/LaunchAgents/org.macports.selfupdate.plist /Library/LaunchDaemons/org.macports.selfupdate.plist

Modify owner and permissions:

sudo chown root:wheel /Library/LaunchDaemons/org.macports.selfupdate.plist
sudo chmod 644 /Library/LaunchDaemons/org.macports.selfupdate.plist

Load the daemon:

sudo launchctl load /Library/LaunchDaemons/org.macports.selfupdate.plist



Such launch daemon is run with root permissions and can update MacPorts data as well as write to /var/log/.