Macos – Launch a daemon after another daemon on Mac OS X

launchdmacosMySQL

How one would create a dependency between two daemons launched on system start?

I would like to launch sonar when the system starts but it requires mysql server to be already up and running.

I didn't find an explicit way to define a process dependency in the launchd plists.

And from the launchd Wikipedia page, there is a very encouraging sentence:

The hardest part to manage during a launchd boot is dependencies.

Sonar daemon:

<?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>WorkingDirectory</key>
    <string>/usr/local/Sonar/sonarinstall</string>
    <key>Label</key>
    <string>org.sonarsource.sonar</string>
    <key>KeepAlive</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>bin/macosx-universal-64/sonar.sh</string>
        <string>start</string>
    </array>
    <key>UserName</key>
    <string>server1</string>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>  

MySQL daemon:

<?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>homebrew.mxcl.mysql</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/opt/mysql/bin/mysqld_safe</string>
    <string>--bind-address=127.0.0.1</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/usr/local/var</string>
</dict>
</plist>

Best Answer

You might be able to reach your goal by altering the KeepAlive-Part of your MySQL-plist.

The following should (in theory) provide what you are looking for:

<key>KeepAlive</key>
<dict>
    <key>OtherJobEnabled</key>
    <string>org.sonarsource.sonar</string>
</dict>

That should result in the MySQL being started before the sonar server and kept alive as long as the sonar server runs.

For more info have a look at those resources:

If that doesn't work out, Apple will ask you to use InterProcessCommunication (IPC) to get your setup working. But to be honest, thats beyond my knowledge! Someone else might hop in there.

Related Question