How to wait until internet connection, before launching "login item"

My Slack and a few other apps will automatically launch at login on my Macbook, since they are listed under "login items". However, some, like Slack, will launch before the internet is connected, and that causes them to flail. Is there some way to delay launching those apps until the internet is connected?

With systemd/systemctl, you can wait until "network.target" is made before running background process. I wonder if launchctl can do that.


According to the Daemons and Services Programming Guide this is not directly possible via launchd.

Network Availability

If your daemon depends on the network being available, this cannot be handled with dependencies because network interfaces can come and go at any time in OS X. To solve this problem, you should use the network reachability functionality or the dynamic store functionality in the System Configuration framework. This is documented in System Configuration Programming Guidelines and System Configuration Framework Reference. For more information about network reachability, see Determining Reachability and Getting Connected in System Configuration Programming Guidelines.

Apps should really work when network is unavailable rather than just failing but if you want to do it yourself you could remove their startup items and make your own Launch Agent call a script that waits for a connection like this:

  • create a script such as ~/.local/bin/launch_my_programs
#!/bin/bash
while ! ping -c1 -W1 1.1.1.1 &> /dev/null ; do
    sleep 1
done
open /Applications/BBEdit.app # or whatever apps you want
  • create a .plist such as ~/Library/LaunchAgents/my.startup.plist and include full path of your script
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>my.startup</string>
    <key>Program</key>
    <string>/Users/hali/.local/bin/launch_my_programs</string>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>
  • chmod +x ~/.local/bin/launch_my_programs to make your script executable.
  • launchctl load -w ~/Library/LaunchAgents/my.startup.plist to load agent.