MacOS: launchd

MacOS: launchd

Launchd je servis na macOS koji je direktna zamena za crontab, ali dosta moćnija. Ustvari, pošto crontab radi na macOS, treba pomenuti da je i sam cron utility is launched by launchd.

Na linuxu, systemd je naravno ono što je Launchd na macOS, dok Windows za to koristi Task Scheduler. Ali ako mi treba baš jednostavna komanda kojoj se ponavlja izvršavanje, i ako OS podržavam, ja ipak mislim da je crontab idealan jer je jednostavan.

U launchd sve funkcioniše preko .plist fajlova.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cat <<EOF | tee ~/Library/LaunchAgents/com.svko.autocommit.plist
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.svko.autocommit</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/usr/local/bin/autocommit.sh</string>
    </array>
    <key>StartInterval</key>
    <integer>60</integer>
    <key>StandardErrorPath</key>
    <string>/var/log/autocommit_error.log</string>
    <key>StandardOutPath</key>
    <string>/var/log/autocommit.log</string>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
EOF


nano ~/Library/LaunchAgents/com.svko.autocommit.plist
launchctl load -w ~/Library/LaunchAgents/com.svko.autocommit.plist

launchctl list | grep autocommit

tail /var/log/autocommit* -f -n0

Validate the .plist

1
plutil ~/Library/LaunchAgents/com.svko.autocommit.plist

To edit, first:

1
launchctl unload -w ~/Library/LaunchAgents/com.svko.autocommit.plist

To run:

1
2
launchctl start com.svko.autocommit
launchctl stop com.svko.autocommit

launchctl does not have a reload command, so you must unload and load:

1
2
launchctl unload -w ~/Library/LaunchAgents/com.svko.autocommit.plist
launchctl load -w $_

Odličan GUI tool za Launchd je LaunchControl: The launchd GUI koji lako instaliraš sa brew install --cask launchcontrol

The Complete Guide to Cron and Launchd in macOS/Linux | by Shinichi Okada | in Towards Data Science - Freedium

Ima lepi terminal-notifier u brew.

Debugging launchd plist jobs – What do you know?

date 28. Dec 2023 | modified 10. Jun 2024
filename: macOS » Launchd and Crontab
Article Content