1

I’m trying to make a launch agent that starts a Python script. It should run when there’s a network connection but it doesn’t do that. launchctl list says it’s loaded:

launchctl list | grep test.Flopsey.DiscordMusicBot
-   0   test.Flopsey.DiscordMusicBot

When I start it with launchctl start test.Flopsey.DiscordMusicBot it works fine. The .plist file (which is stored under ~/Library/LaunchAgents) looks like this:

<?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>test.Flopsey.DiscordMusicBot</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5</string>
        <string>/path/to/MusicBot/run.py</string>
    </array>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/bin:/usr/bin:/usr/local/bin</string>
    </dict>
    <key>StandardOutPath</key>
    <string>/path/to/MusicBot/log.log</string>
    <key>StandardErrorPath</key>
    <string>/path/to/MusicBot/log.log</string>
    <key>WorkingDirectory</key>
    <string>/path/to/MusicBot</string>
    <key>KeepAlive</key>
    <dict>
        <key>NetworkState</key>
        <true/>
    </dict>
</dict>
</plist>

I’m new to launchd and I followed this tutorial. I think the solution to my problem is very basic but I have no idea what it could be. I’ve also made another similar agent and it works fine (unlike the music bot it’s only one file).

Update

Thanks to @LCC’s comment I realised that using NetworkState doesn’t work anymore on OS X 10.10 and higher. Since the script exits when it can’t connect to the Internet I can just set KeepAlive to <true/> and set up a ThrottleInterval so launchd restarts the script after a cooldown if it couldn’t connect.

Community
  • 1
  • 1
FlopseyFlow
  • 13
  • 1
  • 5
  • is there any logging in your launchagent? it *might* be that your launchagent does start to run, but before networking is fully set-up and ready to go, and then your agent exits. It might be useful to see if the agent fires at all at start-up/login time. – Michael Dautermann Apr 18 '17 at 23:51
  • Which version of OSX do you use? The tutorial you are referring to points out that support for NetworkState has been dropped in OSX 10.10. If you check your agent with LaunchControl (the launchd GUI promoted on that site) you will get a warning too. – LCC Apr 19 '17 at 05:55
  • @MichaelDautermann there’s nothing in the log but Python should print a startup message and/or connection errors, so I think it doesn’t run. But even if it fails to connect it should either retry or throw an error and exit (I’m not sure what the exact behaviour is since the script is not made by me) and launchd would restart it because of the `KeepAlive` key. – FlopseyFlow Apr 19 '17 at 12:34
  • @LCC oh I didn’t see that, thanks. Is there another way to start an agent in Sierra (10.12.4) when there’s a network connection or do I have to do that by myself? – FlopseyFlow Apr 19 '17 at 12:40
  • @FlopseyFlow This sounds like a reasonable approach to check for network changes: https://superuser.com/a/626919 Your script should be started every time such a change is detected. It is left to the script to determine if a network connection is available or not. – LCC Apr 19 '17 at 12:58

0 Answers0