CLI

Run Script every minute

  • Create file, e.g com.user.script.plist in ~/Library/LaunchAgents/

<?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.user.script.connectvpnonwifi</string>
    <key>Program</key>
    <string>/bin/zsh</string>
    <key>ProgramArguments</key>
    <array>
        <string>-c</string>
        <string>/Users/usera/code/HomeServer/Script/connectwifi.sh</string>
    </array>
    <key>StandardOutPath</key>
    <string>/Users/usera/code/HomeServer/Script/myjob.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/usera/code/HomeServer/Script/error.log</string>
    <key>Debug</key>
    <true/>
    <key>StartInterval</key>
    <integer>60</integer>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
  • launchctl load ~/Library/LaunchAgents/com.user.script.plist: load script

  • launchctl unload ~/Library/LaunchAgents/com.user.script.plist: load script

  • launchctl print gui/$(id -u)/com.user.script: check script status

  • plutil -lint ~/Library/LaunchAgents/com.user.script.plist: check script syntax

  • launchctl kickstart -k gui/$(id -u)/com.user.script: start the job manually

  • log show --predicate 'eventMessage contains "script"'

Edit Path settings

  • Path environment variable can be set at the following places:

    • ~/.zshrc

    • ~/.zprofile

    • ~/.zshenv

    • /etc/paths

    • /etc/paths.d/

    • /etc/profile

    • /etc/zshenv

    • exit the terminal or log out and log in to see the new path

  • Check PATH Env Variable

    • printenv | grep PATH

    • launchctl getenv PATH

Run Shell command when starting terminal app

  • Terminal > Settings > Profile > Shell > Startup - Run Command

Reference

  • https://support.apple.com/en-au/guide/terminal/apdc6c1077b-5d5d-4d35-9c19-60f2397b2369/mac

  • https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html#//apple_ref/doc/uid/10000172i-CH1-SW2

  • https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html#//apple_ref/doc/uid/TP40001762-104142

Last updated