2

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:

<?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>com.spidbackup</string>
    <key>Program</key>
    <string>/Users/Study/Library/Scripts/spidbackup.sh</string>
    <key>StandardErrorPath</key>
    <string>/tmp/com.spidbackup.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/com.spidbackup.out</string>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>17</integer>
            <key>Minute</key>
            <integer>11</integer>
        </dict>
    </array>
    <key>WorkingDirectory</key>
    <string>/Users/Study</string>
</dict>
</plist>
  • First, it sounds like this is running as a launch daemon (as root), rather than as a launch agent (under the logged-in user). How exactly did you install and activate it? Also, are you sure you're running the right version of the script (i.e. the updated version with /Users/Study/xxx instead of ~/xxx)? – Gordon Davisson Nov 6 '16 at 1:14
  • Gordon, yes, I've run cat on the file listed in the plist. It is the correct version. It runs fine from the command line. I originally added it as root in /Library/LaunchAgents/. I've tried moving it to ~/Library/LaunchAgents and ensuring root is not the owner. – Mike Nov 6 '16 at 5:25
  • Quick questions...how do you want this file to run, only when the user is logged in or when the computer is on? Do you only want it for one user or for all users – Allan Nov 6 '16 at 11:00
  • @Allan, I want it to run once a month if the computer is on. But I'm the only user and the computer is always on. I realize now that LauncgAgents is the best place for it. – Mike Nov 6 '16 at 20:21
1

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).

  • klanomoath, that was a copy/paste error on the shebang. The # is actually there. I tried copying the text from each file and pasting into a new file, renaming each one and putting them into their respective directories: ~/Library/LaunchAgents/spidbak.sh, ~/Library/Scripts/spidbak.sh. I did the copy/paste in TextWrangler, saved to my docs folder and used cp in Terminal to move them. I made spidbak.sh executable. Still the same problem. I'm quite confused. – Mike Nov 6 '16 at 5:36
  • I just tried moving the script to a new directory which wasn't owned by root. No change. – Mike Nov 6 '16 at 5:45
  • @Mike The proper name is /Users/Study/Library/LaunchAgents/com.spidbackup.plist" – klanomath Nov 6 '16 at 7:55
0

I don't actually have the answer but it was clearly something to do with permissions and ownership. I started over with a new file and a new plist. Everything works as expected. Thanks for all the comments.

You must log in to answer this question.

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