Frage

  • I am trying to create a plist file that is run by launchd.
  • I want it to run every 2 minutes (for now) and it should execute a mysqldump backup.
  • It is used to back up all my local MAMP databases.
  • The script itself:

/Applications/MAMP/Library/bin/mysqldump --opt -u root -proot --host=localhost --all-databases > ~/Dropbox/Development/Shared\ DBs/BACKUPS/all_databases_-'date "+%Y-%m-%d_%H.%M.%S"'.sql

works without issue when entered in to the terminal.

  • I need to automate this backup, so wrote the following plist file, and put it in ~/Library/LaunchAgents/com.localhost.cron.plist

  • Then ran launchctl load ~/Library/LaunchAgents/com.localhost.cron.plist, but it isn't triggering every 2 minutes.

Any idea what I'm doing wrong?

<?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.localhost.cron.plist</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/MAMP/Library/bin/mysqldump --opt -u root -proot --host=localhost --all-databases > ~/Dropbox/Development/Shared\ DBs/BACKUPS/all_databases_-`date "+%Y-%m-%d_%H.%M.%S"`.sql</string>
    </array>
    <key>StartInterval</key>
    <integer>120</integer>
</dict>
</plist>

Notes

I'm using OS X Mavericks and the latest MAMP.

Thanks in advance.

Edit

The chosen answer below answers my question well, but I found what I think is a better approach (choice is yours). It is available here: Send Stderr to dev/null but if no error occurs, continue on

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top