1

I am having trouble getting a bash script running through a plist that will be launched through launchctl.

When I run it normally through bash by running sh script.sh everything works fine and it runs as it should and no complaints about jq.

However when I try launching that same script through a plist launch agent with launchctl, it still starts fine, but it exits with an error code 127, from which I can see in my error logs that it throws:

line 9: jq: command not found

jq is installed through brew and I also installed the binary manually in usr/local/bin, but that also does not seem to work. I have no idea whats going on. I will put some of the relevant code snippets here:

The code lines where it fails in spotify-slack.sh

JSON=$(echo '{}' | jq --arg SONG "$SONG" --arg ARTIST "$ARTIST" --arg EMOJI "$EMOJI" '.profile.status_text=$ARTIST+" - "+$SONG | .profile.status_emoji=$EMOJI')

curl -X POST --data "$JSON" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json; charset=utf-8" --silent -o /dev/null https://slack.com/api/users.profile.set | jq 'del(.profile)'

The plist file

<?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.user.spotify-slack</string>
        <key>ProgramArguments</key>
        <array>
            <string>sh</string>
            <string>/Users/brulmanj/src/spotify-slack/spotify-slack.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>60</integer>
        <key>StandardErrorPath</key>
        <string>/Users/brulmanj/src/spotify-slack/.logs/error.log</string>
        <key>StandardOutPath</key>
        <string>/Users/brulmanj/src/spotify-slack/.logs/output.log</string>
    </dict>
</plist>

Thanks in advance!

jbrulmans
  • 975
  • 1
  • 11
  • 32
  • What's the PATH in the script? Where is your copy of jq installed? Presumably it's coming from somewhere like `/usr/local/bin` or `/opt/homebrew/bin` or `/Users/brulmanj/.nix-profile/bin`; make sure that location, wherever it is, is in the PATH that launchd uses when it's invoking your script. – Charles Duffy Apr 05 '23 at 15:59
  • 1
    See the answers to https://serverfault.com/questions/111391/use-an-environment-variable-in-a-launchd-script describing how to add an `EnvironmentVariables` section to your plist file so you can set an appropriate PATH value. Or just explicitly configure the PATH inside the script itself, if you so choose. – Charles Duffy Apr 05 '23 at 16:00
  • Thanks for the help @CharlesDuffy, it was indeed setting an `EnvironmentVariables` with the `PATH` included. Much appreciated – jbrulmans Apr 06 '23 at 07:31

0 Answers0