How can I debug a Launchd script that doesn't run on startup?

Viewed 39433

I have some Launchd scripts from homebrew. However I have to manually run them when I restart my computer:

launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

<?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>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>com.mysql.mysqld</string>
  <key>Program</key>
  <string>/Users/dash/.local/Cellar/mysql/5.1.49/bin/mysqld_safe</string>
  <key>RunAtLoad</key>
  <true/>
  <key>UserName</key>
  <string>dash</string>
  <key>WorkingDirectory</key>
  <string>/Users/dash/.local/var</string>
</dict>
</plist>

I thought this should happen on startup. What am I missing?

6 Answers

For me, other solutions so far do not help me. My problem is kinda hard to debug, because the plist file is correct, the script is running fine alone in a terminal. Everything looks fine, but doesn't work.

I checked the log file by executing

tail -f /var/log/system.log

And then by unloading and loading the service again with the commands:

launchctl unload ~/Library/LaunchAgents/example.plist
launchctl load ~/Library/LaunchAgents/example.plist

I found an error message from the log file:

Program specified by service is not a Mach-O executable file.

What does it really mean? I didn't google. But, I feel it's because I didn't add #!/bin/bash at the beginning of the shell script. Because I am lazy to add this line sometimes.

After adding the heading, everything works fine.

From OSX Yosemite 10.10 Onwards launchctl commands are changed.

Following commands will be required to start service automatically at the reboot.

sudo launchctl bootstrap system /Library/LaunchDaemons/${YOUR_SERVICE_NAME}.plist
sudo launchctl enable system/${YOUR_SERVICE_NAME}
sudo launchctl kickstart -kp system/${YOUR_SERVICE_NAME}

Note: It will launch service with user root and start system-wide.

References: Launchctl Man Page(https://ss64.com/osx/launchctl.html)

Related