Your ProgramArgument
value is not correct. You are supposed to provide an extra string for every argument. This should work better:
<key>ProgramArguments</key>
<array>
<string>/Applications/MAMP/Library/bin/mysqldump</string>
<string>--opt</string>
<string>-u</string>
<string>root</string>
<string>-proot</string>
<string>--host=localhost</string>
<string>--all-databases</string>
</array>
Output redirection is done with the key StandardOutPath
, but shell expansion of ~
won't work. Also launchd won't allow your shell command to create a dynamic file name. You're better off wrapping all of this in a shell script and calling it from launchd instead.
In order to run this script only while MySQL is running you could use the KeepAlive
key like this:
<key>KeepAlive</key>
<dict>
<key>PathState</key>
<dict>
<key>/var/run/mysql.pid</key>
<true/>
</dict>
</dict>
This approach assumes that your MySQL instance writes its current PID to the file /var/run/mysql.pid
. The file will only be there while MySQL is running.
As you correctly pointed out StartInterval
and KeepAlive
don't work together. The script would be fired regardless of the existence of the PID file. The correct approach is to use ThrottleInterval
instead of StartInterval
. This key determines how much time has to pass between starts. Use it like this:
<ThrottleInterval>
<integer>120</integer>
Have a look at launchd.plist(5)
. It explains all the options in great detail.