Saturday, October 5, 2024

plist – My launchd scripts usually are not run after boot however after login

I need to begin a shell script when a MacOS (14.2.1) machine boots. However the scripts is executed solely once I log in, circuitously after the system has began.

I wrote a plist definition like this:

> ls -l /Library/LaunchDaemons/com.foobar.justLog.plist
-rw-r--r--@ 1 root  wheel  397 Jan 25 21:06 /Library/LaunchDaemons/com.foobar.justLog.plist
> cat /Library/LaunchDaemons/com.foobar.justLog.plist
<?xml model="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Pc//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist model="1.0">
    <dict>
        <key>Label</key>
        <string>com.foobar.justLog</string>
        <key>RunAtLoad</key>
        <true/>
        <key>Program</key>
        <string>/usr/native/bin/justLog.sh</string>
    </dict>
</plist>
>

The referenced shell script seems like this:

> ls -l /usr/native/bin/justLog.sh
-rwxr-xr-x@ 1 root  wheel  105 Jan 25 14:46 /usr/native/bin/justLog.sh
> cat /usr/native/bin/justLog.sh
#!/bin/bash
whereas true ;do
    echo "Began script $0 as person $(whoami) in $PWD ($(date))"
    sleep 120
executed
>

Then I shutdown the mac, waited 2 minutes and turned on the mac (from 21:46:40 to 21:46:55). Then I waited a minute to present the scripts a while. Then I logged on with my default person (from 21:48:00 to 21:48:36). Afterwards I examine the information in /var/log/com.apple.xpc.launchd and I see that there aren’t any entries from launchd in minutes 21:46, 21:47. However thats precisely the time the place the system boots. Just for the minute the place I logged in I discover entries from launchd. It seems like launchd does nothing earlier than the primary person logs in.

> for i in 5 6 7 8 ;do echo "inspecting minute: 21:4$i"; grep "2024-01-25 21:4${i}:" /var/log/com.apple.xpc.launchd/launchd.log{.2,.1,} /var/log/* 2>/dev/null | wc -l ;executed
inspecting minute: 21:45
   11747
inspecting minute: 21:46
       0
inspecting minute: 21:47
       0
inspecting minute: 21:48
   21150
>

Can anybody clarify why my script just isn’t executed earlier than I log in?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles