Trouble creating directory from script run by launchd I have a simple script that creates a new directory based on the date then copies a set of files to that directory.  It runs fine from Terminal.  When I set it up to run as a UserAgent it fails.  The error log shows that has changed to paths from /Users/Study/xxx to /var/root/xxx. I've tried running it as root from Terminal and it still works.  I've ensured root is not the owner of the script file.  I've changed my original path of ~/xxx to the /Users/Study/xxx  but for some reason when run from launchd it changes the path.  Any ideas?

script:

!/bin/bash

MO=$(date +%d%B%Y)
mkdir /Users/Study/Dropbox/SPID_backups/$MO
cp -r /Users/Study/WebDevelopment/SPID/*.* /Users/Study/Dropbox/SPID_backups/$MO


launchctl plist:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;com.spidbackup&lt;/string&gt;
    &lt;key&gt;Program&lt;/key&gt;
    &lt;string&gt;/Users/Study/Library/Scripts/spidbackup.sh&lt;/string&gt;
    &lt;key&gt;StandardErrorPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.spidbackup.err&lt;/string&gt;
    &lt;key&gt;StandardOutPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.spidbackup.out&lt;/string&gt;
    &lt;key&gt;StartCalendarInterval&lt;/key&gt;
    &lt;array&gt;
        &lt;dict&gt;
            &lt;key&gt;Hour&lt;/key&gt;
            &lt;integer&gt;17&lt;/integer&gt;
            &lt;key&gt;Minute&lt;/key&gt;
            &lt;integer&gt;11&lt;/integer&gt;
        &lt;/dict&gt;
    &lt;/array&gt;
    &lt;key&gt;WorkingDirectory&lt;/key&gt;
    &lt;string&gt;/Users/Study&lt;/string&gt;
&lt;/dict&gt;
&lt;/plist&gt;
You backup task is (very) user-specific and no system service, so the proper location for the launch agent is ~/Library/LaunchAgents/. If you put the plist in /Library/LaunchAgents it will run on behalf of every user logged in at ~ 5.11pm - which will fail for most users because they probably can't access some or most of your user folders/files.



Use a proper shebang in the script #!/bin/bash.. and make the script executable: chmod +x /Users/Study/Library/Scripts/spidbackup.sh.

Completely remove the plist from the launchctl db with sudo launchctl remove com.spidbackup and launchctl remove com.spidbackup. Move the plist to /Users/Study/Library/LaunchAgents/, chown/chmod it to your user and remove the key WorkingDirectory and the associated string.

Finally reload the agent with:

launchctl load /Users/Study/Library/LaunchAgents/com.spidbackup.plist 




The proper permissions/ownerships for the files are finally:

ls -l /Users/Study/Library/Scripts/spidbackup.sh
-rwxr-xr-x  1 study  staff  ... spidbackup.sh


or

-rwx------  1 study  staff  ... spidbackup.sh


and

ls -l /Users/Study/Library/LaunchAgents/com.spidbackup.plist 
-rw-r--r--  1 study  staff  ... com.spidbackup.plist


assuming Study is your user name.

Please also check capitalization (e.g. study vs. Study).