1

I've written a launch agent:

<?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>local.mylaunchagent.plist</string>
        <key>ProgramArguments</key>
        <array>
            <string>~/my_script.sh</string>
        </array>
        <key>StartInterval</key>
        <integer>12</integer>
        <key>StandardOutPath</key>
        <string>~/my_log_file</string>
        <key>StandardErrorPath</key>
        <string>~/my_err_file</string>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>

It works, but it runs every 22 seconds instead of every 12 seconds, because ~/my_script.sh takes 10 seconds to finish.

launchd seems to be adding the interval time and the execution time together, even though I want the program to be run every interval. I don't want launchd to wait for it to finish.

So I tried running the program in the background:

<key>ProgramArguments</key>
<array>
    <string>/bin/zsh</string>
    <string>-c</string>
    <string>~/my_script.sh & disown</string>
</array>

But now I don't get any output at all, so I suspect that launchd kills all spawned processes as soon as the process that it launched exits.

So my question is, how can I start a slow command (takes an unpredictable 10+ seconds to finish) every 12 seconds reliably?

4
  • 1
    Look up StartCalendarInterval in the man page.
    – nohillside
    Commented Oct 31 at 18:55
  • The documentation says "If the job is running during an interval firing, that interval firing will likewise be missed." So there should only be a problem if the script takes more than 12 seconds. 10 seconds shouldn't be an issue.
    – Barmar
    Commented Oct 31 at 21:36
  • Your description suggests that the interval timer doesn't start until after the job finishes, but that's not how it's supposed to work. If a job takes more than 12 seconds, it should run every 24 seconds, not every 22.
    – Barmar
    Commented Oct 31 at 21:37
  • @Barmar well, that's not what I've observed
    – user150109
    Commented Oct 31 at 22:03

1 Answer 1

1

It’s an error for a Launch Daemon to fork into the background. The right way to do what you want is to start a long-running foreground process that periodically spawns a background process, optionally after checking that all previous subprocesses have exited.

To ensure that the job is always running, you need to set a suitable value for the KeepAlive key, such as True.

4
  • I don't need to confirm that all previous subprocesses have exited. I'm a bit worried about doing it this way in case the long-running foreground process unexpectedly exits. I thought it would be nice to rely on launchd
    – user150109
    Commented Oct 31 at 23:02
  • Then don’t check. The parent job will be restarted immediately if it quits.
    – Linc Davis
    Commented Oct 31 at 23:04
  • Which process should manage the parent job and automatically restart it?
    – user150109
    Commented Oct 31 at 23:07
  • That’s what launchd does. I strongly suggest that you familiarize yourself with the launchd.plist(5) man page, or use one of the third-party apps that guide you through creating a job. Making arbitrary assumptions about how it works won’t serve you well.
    – Linc Davis
    Commented Oct 31 at 23:11

You must log in to answer this question.