42

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?

3
  • I don't think this will have any effect (hence, I'm not making it an answer), but try running it without the "-w" flag for a test (i.e. "launchctl load ~/Library/LaunchAgents/com.mysql.mysqld.plist") and then restart. Or, maybe try the full file path to the plist (e.g. /Users/{you}/Library/LaunchAgents/com.mysql.mysqld.plist). Just guessing here. Commented Jul 31, 2011 at 3:10
  • 2
    Not really an answer, but I'm sure that LaunchControl will tell you why. Commented Sep 5, 2013 at 14:30
  • Check out my answer to a similar issue. Commented May 27, 2014 at 0:22

6 Answers 6

50

Best way I found to debug, in your plist:

<key>StandardErrorPath</key>
<string>/tmp/mycommand.err</string>
<key>StandardOutPath</key>
<string>/tmp/mycommand.out</string>

Open Console app, in "All Messages" you should see entries when your app fails or succeeds. Like this:

4/28/15 10:43:19.938 AM com.apple.xpc.launchd[1]: (mycommand[18704]) Service exited with abnormal code: 1

The issue I had was with ProgramArguments takes each item of command as <string> item in the array.

EDIT: In my case, generating a simple wrapper for shell script worked even better. This script sets up basic folder structure to make a shell script into an OS X "app" - https://gist.github.com/mathiasbynens/674099. This might work better for your mysql -u arg1 command.

Sign up to request clarification or add additional context in comments.

Comments

9

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.

2 Comments

Generally it means, your OS does not know how to execute program. Possible that your binary was compiled for different OS or architecture. You should be able to reproduce your error message by running from regular MacOS terminal.
Every binary has a header that defines what kind of a binary it is. That is the generic error about the binary type being unknown. Of course, it was not a binary at all, it was a script. You should absolutely use the script header to tell the system which command interpreter to use. You don't have to use bash it could also be #! /bin/zsh
4

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)

Comments

2

The start command expects the job Label as it's argument, so you would start it using the following...

launchctl start com.myfile.hostname.plist

To stop, simply do the following...

launchctl stop com.myfile.hostname.plist

Once all your testing is complete you would log out then in to load it or if your plist file is in the users Library folder type the following...

launchctl load ~/Library/LaunchAgents/com.myfile.hostname.plist

Comments

1

One possibility: Look in the directory:

/private/var/db/launchd.db/

and fine the "com.apple.launchd.peruser.###" file for your user. Open that and see if there is a entry like:

<key>com.mysql.mysqld.plist</key>
<dict>
    <key>Disabled</key>
    <true/>
</dict>

If so, try setting it to <false/>. Another file to look in for the same thing is:

/private/var/db/launchd.db/com.apple.launchd/overrides.plist

2 Comments

That looked promising, but alas it was <false/>
Maybe try cutting it out all together? I don't see how that would make any difference, but I'm grasping at straws.
0

Try renaming it. Change the filename to:

~/Library/LaunchAgents/com.mysql.mysqld2.plist

and the Label section in the plist to:

<key>Label</key>
<string>com.mysql.mysqld2</string>

If you save a backup copy, make sure to move it outside of your ~/Library/LaunchAgents/ directory.

Finally, instead of using launchctl to load it, just logout and log back in. That will let launchd pick it up from your "LaunchAgents" directory by itself and take one more variable (i.e. launchctl) out of the mix.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.