1

I'm trying to automatically execute a Python script every day, but it requires an Internet connection (to send emails, etc.). So I fixed the date and hour at which it needs to be executed, but if there's no network available, I want it to be executed as soon as it finds a network. I thought that the following would do just what I need:

<?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.example</string>

    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/python</string>
        <string>/path/to/my/script.py</string>
    </array>

    <key>StartCalendarInterval</key>
    <dict>
      <key>Hour</key>
      <integer>10</integer>
      <key>Minute</key>
      <integer>30</integer>
    </dict>

    <key>KeepAlive</key>
    <dict>
      <key>NetworkState</key>
      <true/>
    </dict>

  </dict>
</plist>

It works fine when I have an Internet connection at 10:30, but when I don't have network available but only get it back at, say, 11:00, it does not execute the script and does nothing for the rest of the day.

What did I do wrong?

Many thanks,

1 Answer 1

3

Check the manpage (man launchd.plist) to see if NetworkState is implemented.

For me, on macOS Mojave, it is not:

KeepAlive <boolean or dictionary of stuff>

This optional key is used to control whether your job is to be kept continuously running
or to let demand and conditions control the invocation. The default is false and therefore
only demand will start the job. The value may be set to true to unconditionally keep the
job alive. Alternatively, a dictionary of conditions may be specified to selectively control
whether launchd keeps a job alive or not. If multiple keys are provided, launchd ORs them,
thus providing maximum flexibility to the job to refine the logic and stall if necessary.
If launchd finds no reason to restart the job, it falls back on demand based invocation.
Jobs that exit quickly and frequently when configured to be kept alive will be throttled
to conserve system resources.

    NetworkState <boolean>
    This key is no longer implemented as it never acted how most users expected.

You must log in to answer this question.

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