Using launchd to run a PHP script

The good old cronjobs are deprecated in macOS and launchd is the preferred way nowadays. It's the first time for me and it took me a while to get it working. At first I didn't realize you need to add multiple elements for a program with parameters. Secondly, symlinks didn't work for me. I had to use /Users/username instead of the symlink I had (/www/). I'm not sure why.

Here is the plist file I used:

<?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>com.companyname.somename</string>
 
   <key>ProgramArguments</key>
   <array>
    <string>/usr/local/bin/php</string>
    <string>/Users/username/www/foo/artisan</string>
    <string>schedule:run</string>
   </array>
 
  <key>StartInterval</key>
  <integer>60</integer>
 
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

It should be named like "com.companyname.somename.plist". You can place it in $HOME/Library/LaunchAgents and then load it via launchctl load com.companyname.somename.plist.

comments powered by Disqus