5

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.

1
  • What is causing your Internet to take so long to connect?
    – Allan
    Mar 24, 2020 at 3:45

1 Answer 1

7

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.
1
  • 1
    I had a similar requirement but it was related to mounting a share that was only available when connected to VPN. I ended up adding an incrementing counter inside the while and after 20 attempts, sleeping for 20 sec., it breaks out of loop and ends.
    – nickdos
    May 5, 2023 at 6:27

You must log in to answer this question.

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