0

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 ?

2
  • 1
    Your plist file has <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. Feb 1, 2022 at 1:58
  • @GordonDavisson thank you. it is solved. I don't know how to record your contribution to solve this problem. Anyway I thank you very much.
    – hotoku
    Feb 2, 2022 at 9:28

1 Answer 1

2

This is because of the <key>KeepAlive</key><true/> in the .plist file, which tells launchd to auto-restart the program if/when it exits. launchd doesn't like to restart things too often, so by default it'll "throttle" restarts to every 10 seconds (which is what you're seeing). You can either set the key to <false/>, or just remove it entirely (it defaults to false).

If you do want the job restarted under certain conditions, you can specify an array of conditions where it should be restarted. For example, to restart the job if it exits with an error status, you could use this:

<key>KeepAlive</key>
<dict>
    <key>SuccessfulExit</key>
    <false/>
</dict>

(In this case, <key>SuccessfulExit</key><false/> essentially means "if it didn't exit successfully".)

BTW, I know that script is just a stub to test how the job is being run, but I noticed a bug. Since [$(date +"%Y-%m-%d %H:%M:%S")] is not double-quoted, the shell will try to "expand" it into a list of matching filenames. That is, [2022-02-01 09:13:55] would look for files named "2", "0", etc and if it finds any it'll replace that bracket expression with the matching names. So if you get output like this:

2 run
2 run
2 run
...etc

That means there's a file named "2" in whatever happens to be the agent's working directory (probably your home directory). You should either double-quote it, or just include the whole thing in the date format string (again, properly quoted):

date +"[%Y-%m-%d %H:%M:%S] run"
1
  • Thank you for not only your answer but also pointing out the bug in the example script.
    – hotoku
    Feb 2, 2022 at 21:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .