Get launchd to run task when network connects or reconnects

后端 未结 1 1598
长发绾君心
长发绾君心 2021-01-24 19:40

I run a custom python script to update various dynamic DNS servers through launchctl and it runs every 15 minutes. This is both overkill and underkill.

It would be nice

1条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-24 20:19

    I don't think launchd has any built-in trigger for the network coming up, but you can monitor file paths for changes. So if we can find a file that is updated when the network connects, we can implement the desired functionality.

    One such file is /var/run/resolv.conf - this file is written whenever the computer renews a DHCP lease. It will be updated when you first connect, and periodically as the DHCP lease is renewed (the timing is set by the DHCP server).

    Here is an example launchd plist file.

    
    
    
    
        Label
        org.example.on-dhcp-renew
        ProgramArguments
        
            /Users/darrin/bin/on-dhcp-renew.sh
        
        WatchPaths
        
            /var/run/resolv.conf
        
    
    
    

    This will only work if you use DHCP addressing for your LAN - as the DHCP client is the program that updates your resolv.conf file.

    0 讨论(0)
提交回复
热议问题