Im trying to add a LaunchDeamon on MacOS that is executed on startup.
I made a .sh script which works on direct execution. Its located at /usr/local/libexec/scripts/startup/
This is my script: startup.sh
#!bin/bash
# Check if deamon is running
if [ "$(ps -ef | grep -v grep | grep clamd | wc -l)" -le 0 ]
then
#Start deamon
/opt/homebrew/Cellar/clamav/0.104.2/sbin/clamd
echo "clamd started"
else
echo "clamd already running"
fi
Also made a launchd file to run it on startup that looks like this. Its located at /Library/LaunchDaemons/
com.startup.plist
<?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>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/bin:/usr/bin:/usr/local/bin</string>
</dict>
<key>Label</key>
<string>com.startup</string>
<key>Program</key>
<string>/usr/local/libexec/scripts/startup/startup.sh</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/startup-scripts.stdout</string>
<key>StandardErrorPath</key>
<string>/tmp/startup-scripts.stderr</string>
</dict>
</plist>
Now I want to add it to the launchctl list.
sudo launchctl load -w /Library/LaunchDaemons/com.startup.plist
Im looking through the list with sudo launchctl list | grep com.startup
and it does exist:
- 78 com.startup
Unfortunately when restarting the computer it is not running the script.. There is no output in any of the stdin/out/err files.
Any suggestions why its not running on startup?
Tell me, if I can provide more infos
I was able to get it running by putting #!/bin/sh to the first line of code. I was having it at line 2..
Also I was deleting all the StandardPath's. They are apparantly only for LaunchAgents.
The tool LaunchControl is a GUI for LaunchAgents/Deamons. There I was able to get a proper error message. Its installable via brew