I want to execute a script in every 60 seconds. I made a .plist file as follows
<?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>info.hotoku.launch-sample</string>
<key>ProgramArguments</key>
<array>
<string>/Users/hotoku/projects/hotoku/launch-sample/run</string>
</array>
<key>StandardErrorPath</key>
<string>/Users/hotoku/projects/hotoku/launch-sample/stderr</string>
<key>StandardOutPath</key>
<string>/Users/hotoku/projects/hotoku/launch-sample/stdout</string>
<key>WorkingDirectory</key>
<string>/Users/hotoku/projects/hotoku/launch-sample</string>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>
The contents of the specified script is the following:
#!/bin/bash
echo [$(date +"%Y-%m-%d %H:%M:%S")] run
That is, the script just outputs the time of execution to its stdout (of course, this is an example).
I put the .plist file in my ~/Library/LaunchAgent
. Waiting several minutes and checking the contents of StandardOutPath
, I got this.
$ cat /Users/hotoku/projects/hotoku/launch-sample/stdout
[2022-02-01 09:13:55] run
[2022-02-01 09:14:05] run
[2022-02-01 09:14:15] run
[2022-02-01 09:14:25] run
[2022-02-01 09:14:35] run
[2022-02-01 09:14:46] run
[2022-02-01 09:14:56] run
[2022-02-01 09:15:06] run
[2022-02-01 09:15:16] run
[2022-02-01 09:15:26] run
It looks like my launch agent launches every 10 seconds even though it is given the start interval value as 60.
Could anyone fix the problem ?
<key>KeepAlive</key><true/>
, which tells launchd to relaunch the job immediately if it exits (but launchd will "throttle" it and refuse to restart it faster than every 10 seconds). See this question.