0

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

6
  • The two things that I see immediately are that you have a system script in your user directory, which can cause trouble due to privacy protection (though I don't think ~/scripts would be on the protected list), and that you seem to be treating StandardInPath as if it pointed to an output file rather than an input file (I'd just remove this key). Beyond that, check the system logs and sudo launchctl list com.startup. Commented Mar 13, 2022 at 16:52
  • Thanks, I used sudo chmod -x on the script and thought this should be okay. Can you recommand a directory for my scripts? Commented Mar 14, 2022 at 8:33
  • 1
    The privacy protection system in macOS is independent of regular file permissions. For background/auto-run executables (including scripts), I'd generally recommend creating /usr/local/libexec and putting them there. Commented Mar 14, 2022 at 9:14
  • @GordonDavisson Unfortunately none of your tipps worked. I moved my script to /usr/local/libexec/scripts/startup/ and delete the standardInPath Key from the launchd file. Is there any way to get a log of whats happening? the StandardIn/Error files are still empty.. Commented Mar 15, 2022 at 19:52
  • The exit code of 78 (listed in the sudo launchctl list output) indicates some sort of problem with the executable file, or maybe the I/O files; see this and this. I notice that the shebang line in your script has a typo: #!bin/bash should be #!/bin/bash; maybe that's the cause? Commented Mar 15, 2022 at 20:38

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.