MacOS – Using launchd to update MacPorts

launchdmacosmacports

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:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.macports.selfupdate</string>
<key>ProgramArguments</key>
<array>
    <string>/opt/local/bin/port</string>
    <string>selfupdate</string>
</array>
<key>StandardOutPath</key>
<string>/var/log/org.macports.selfupdate.log</string>
<key>StandardErrorPath</key>
<string>/var/log/org.macports.selfupdate.err</string>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>604800</integer><!-- 1 week -->
</dict>
</plist>

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?

Best Answer

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/.