I want to start a shell script during the boot of a MacOS (14.2.1) machine. But the scripts is executed only when I log in, not directly after the system has started.
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 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>com.foobar.justLog</string>
<key>RunAtLoad</key>
<true/>
<key>Program</key>
<string>/usr/local/bin/justLog.sh</string>
</dict>
</plist>
>
The referenced shell script looks like this:
> ls -l /usr/local/bin/justLog.sh
-rwxr-xr-x@ 1 root wheel 105 Jan 25 14:46 /usr/local/bin/justLog.sh
> cat /usr/local/bin/justLog.sh
#!/bin/bash
while true ;do
echo "Started script $0 as user $(whoami) in $PWD ($(date))"
sleep 120
done
>
Then I shutdown the mac and restarted it at 21:46:40. I waited until 21:48:00 before I logged on with my default user. I was expecting my script to be run after the machine startet. But when I check the files in /var/log/com.apple.xpc.launchd I see that there are no entries from launchd during the initial boot. It looks like launchd does nothing before the first user logs in. That's not the behaviour I would expect from a script to be run when the system boots.
> 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 ;done
inspecting minute: 21:45
11747
inspecting minute: 21:46
0
inspecting minute: 21:47
0
inspecting minute: 21:48
21150
>
Can anyone explain why my script is not executed before I log in?