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

Fluqz
  • 349
  • 1
  • 4
  • 17
  • 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](https://apple.stackexchange.com/questions/332673/what-and-how-does-macos-mojave-implement-to-restrict-applications-access-to-pers) (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`. – Gordon Davisson Mar 13 '22 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? – Fluqz Mar 14 '22 at 08: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. – Gordon Davisson Mar 14 '22 at 09: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.. – Fluqz Mar 15 '22 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](https://stackoverflow.com/questions/34215527/what-does-launchd-status-78-mean-why-my-user-agent-not-running) and [this](https://stackoverflow.com/questions/54503366/launchctl-cant-run-the-binary-and-exits-with-a-code-78). I notice that the shebang line in your script has a typo: `#!bin/bash` should be `#!/bin/bash`; maybe that's the cause? – Gordon Davisson Mar 15 '22 at 20:38
  • Thanks a lot! I was able to find the issue with LaunchControl GUI. I was having #!/bin/sh on the second line instead of the first. Never would I have thought this was an issue!? – Fluqz Mar 15 '22 at 21:01

1 Answers1

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

Fluqz
  • 349
  • 1
  • 4
  • 17