3

I am trying to get a script to run from launchd at regular intervals. The service loads but does not run the script.

The script itself is a very simple test script as follows:

echo "Hello World!" >> /Users/rhiannon/Library/Application\ Support/Script\ Testing/test.txt

The script is saved as /Users/rhiannon/Library/Application\ Support/Script\ Testing/test-launchd.sh (with permissions rwxr-xr-x) and happily runs from the command line.

I have a plist file as follows:

<?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>Label</key>
        <string>uk.co.myco.timedshelltests</string>
        <key>Program</key> 
        <string>/Users/rhiannon/Library/Application\ Support/Script\ Testing/test-launchd.sh</string> 
        <key>StartInterval</key>
        <integer>60</integer>
    </dict>
</plist>

The plist file is saved as /Users/rhiannon/Library/LaunchAgents/uk.co.myco.timedshelltests.plist with permissions rw-r--r--.

I load the service using

launchctl load uk.co.myco.timedshelltests.plist

With logging set with sudo launchctl log level debug, I see the following error in the system log:

com.apple.xpc.launchd[1] (uk.co.myco.timedshelltests[13505]): Could not find and/or execute program specified by service: 2: No such file or directory: /Users/rhiannon/Library/Application\ Support/Script\ Testing/test-launchd.sh
com.apple.xpc.launchd[1] (uk.co.myco.timedshelltests[13505]): Service setup event to handle failure and will not launch until it fires.
com.apple.xpc.launchd[1] (uk.co.myco.timedshelltests[13505]): Service exited with abnormal code: 78

I'm pretty certain the permissions are correct, and I know the path is correct. What else could be wrong?

5
  • Have you tried sending the "Hello World" string to some other location than /Library/Application\ Support/Script\ Testing, perhaps to the Desktop?
    – IconDaemon
    Apr 25, 2019 at 15:51
  • 1
    Also, you may need to explicitly put #!/bin/sh as the first line of the script. launchd doesn't know which shell to use until you tell it which one!
    – IconDaemon
    Apr 25, 2019 at 15:55
  • 2
    Note: the escape character,`\`, shows in the error message. Remove them from the Program key string -in other words leave the spaces unquoted.
    – fd0
    Apr 25, 2019 at 15:58
  • @IconDaemon I do have #!/bin/bash as the first line of the script; just omitted it from the above for brevity.
    – Rhiannon
    Apr 26, 2019 at 8:15
  • @fd0 that fixed it, thanks! If you make that comment an answer I'll mark it as correct.
    – Rhiannon
    Apr 26, 2019 at 8:23

1 Answer 1

4

Launchd does not perform word splitting. Remove the escape characters \ from the following line-

<string>/Users/rhiannon/Library/Application\ Support/Script\ Testing/test-launchd.sh</string> 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .