0

I am trying to accomplish running a script on startup on OSX. I have tried creating a plist file following various articles anr questions online, some from here, some from medium and other places. But I just can't seem to get it right. I want the script to run anytime the computer is started, regardless of which user logs on. The script is a simple bash script which checks for the existence of an object in an S3 bucket, and if that object exists, it continues and then obviously runs the rest of the script (in this case, a cleanup script).

Here is my plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.$
<plist version="1.0">
<dict>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
    </dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.cleanup</string>
    <key>Program</key>
    <string>/bin/zsh</string>
    <key>ProgramArguments</key>
    <string>/Users/mlove/Documents/gitpersonal/ejector-seat/cleanup</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/startup.stderr</string>
    <key>StandardOutPath</key>
    <string>/tmp/startup.stdout</string>
    <key>UserName</key>
    <string>mlove</string>
</dict>
</plist>

and here is the script I want to invoke:

#!/bin/zsh
aws s3api wait object-exists --bucket ejector-seat --key button-pressed  --profile parachute
rm -rf /Users/${USER}/Desktop/test/*

Edit: I missed out my error logs which are full of the following: /bin/zsh: can't open input file: /Users/mlove/Documents/gitpersonal/ejector-seat/cleanup

I tried making the script 777 just for testing but still getting the error, and if I run the script manually by simply doing ./cleanup it runs fine, so I know that the script itself is ok, so I think it is something to do with the way I am invoking it inside the plist file.

I must be missing something simple I'm sure, any assistance would be appreciated!

Mattrix
  • 1
  • 1
  • You haven't set `PATH` in your script so it probably can't find `aws`. Did you check your logs in `/tmp`? Why have you coded `$USER` like a variable when it always runs as `mlove`? – Mark Setchell Apr 07 '20 at 15:08
  • Hi, I originally was writing it to be user agnostic so it didnt matter what my user account was, however i might need to change this. I should have also mentioned that in the startup.stderr log, i can see `/bin/zsh: can't open input file: /Users/mlove/Documents/gitpersonal/ejector-seat/cleanup` I have tried making the script 777 just for testing but still get the same error. If i run the script manually by just simply `./cleanup` it works fine. – Mattrix Apr 07 '20 at 15:26
  • Try copying the exact path to your script from the error message and running `ls -l $(pbpaste)` to see if your path is correct. – Mark Setchell Apr 07 '20 at 15:55
  • seems to be a valid path `ls -l $(pbpaste) -rwxr-xr-x 1 mlove staff 388 7 Apr 10:37 /Users/mlove/Documents/gitpersonal/ejector-seat/cleanup` – Mattrix Apr 07 '20 at 15:56
  • I note the permissions aren't `777` per your `chmod`.... – Mark Setchell Apr 07 '20 at 16:15
  • You also appear to have a stray/superfluous colon (`:`) at the end of the PATH in your plist file. – Mark Setchell Apr 07 '20 at 16:17
  • Thank you for the suggestion I've removed the `:`. I changed the permissions back again since changing them to 777 did not seem to help. I will continue playing with it and see if i can manage to work out what the issue is. I've seen many people manage to accomplish this online using a similar method so there must be something I am missing somewhere. – Mattrix Apr 07 '20 at 17:12
  • 1
    You could try removing the `Program` -> `/bin/zsh` part as your script already has a shebang. One other thing, is that you could set the `WorkingDirectory` to something nearer where you want stuff executed. – Mark Setchell Apr 07 '20 at 17:45

0 Answers0