I have a zsh script that uses rsync
to move files from an EC2 instance to my local machine, then copies those files to a Box drive. I have a Mac, so I am trying to use launchd to automate this sync to happen everyday. This 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.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.username.pipelines.syncdata</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/Users/username/.pyenv/versions/anaconda3-2020.11/bin:/Users/username/.pyenv/versions/anaconda3-2020.11/condabin:/Users/username/.pyenv/shims:/Users/username/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/Desktop/geckodriver:/opt/X11/bin</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>/bin/zsh</string>
<string>/Users/username/Desktop/repos/china/zzz_pipelines/sync_data.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>11</integer>
<key>Minute</key>
<integer>15</integer>
</dict>
<key>StandardErrorPath</key>
<string>/Users/username/Desktop/syncdata.err</string>
<key>StandardOutPath</key>
<string>/Users/username/Desktop/syncdata.out</string>
</dict>
</plist>
This is my zsh script, which runs just fine if I paste the file path for the zsh script directly into my terminal:
#!/bin/zsh
########################################
## SCRIPT TO SYNC DATA
## /Users/username/Desktop/repos/china/zzz_pipelines/sync_data.sh
##
## (1) Syncs data from EC2 to Local
## (2) Syncs data from Local to Box
########################################
## PARAMS ----------------------------------------------------------------
# these should be within the repo
# should be subdirs with data we want to move from ec2 to local
SYNCFROMAWS=(
"csrc/raw_data/csrc_ss2015/"
)
# these are files we want to sync from local to Box
# before the | is subdir of `china` repo locally
# after the | is subdir of `china_data` in Box
SYNCTOBOX=(
"csrc/raw_data|csrc"
)
## Logging
BASHLOG_DIR="/Users/username/Desktop/repos/china/zzz_pipelines/sync_data_logs"
BASHLOG="${BASHLOG_DIR}/$(date +'%Y%m%d_%H%M%S')_syncdata.log"
printf "Log File - " > $BASHLOG
date >> $BASHLOG
echo "---------------------------------------------------------------" >> $BASHLOG
EC2INFO="ubuntu@12.123.123.1231"
EC2HOME="/home/ubuntu"
REPONAME="Desktop/repos/china"
BOXDATA="/Users/username/Box/china_data"
## sync from ec2 to local
echo "----------------- SYNCING FROM EC2 to LOCAL -----------------" >> $BASHLOG
for x in ${SYNCFROMAWS[@]}; do
# get the name of the directory for the news source with raw data
FROMDIR="${EC2INFO}:${EC2HOME}/${REPONAME}/${x}"
TODIR="/Users/username/${REPONAME}/${x}"
COMMAND="/usr/bin/rsync -hvrPt -e \"ssh -i /Users/username/.ssh/pemkey.pem\" ${FROMDIR} ${TODIR}"
echo "---------------------------------------------------------------" >> $BASHLOG
echo $COMMAND >> $BASHLOG # to run the script
eval $COMMAND >> $BASHLOG
done
## sync from local to Box
echo "----------------- SYNCING FROM LOCAL to BOX -----------------" >> $BASHLOG
for y in ${SYNCTOBOX[@]}; do
FROM="$(echo ${y} | cut -d'|' -f1)"
TO="$(echo ${y} | cut -d'|' -f2)"
# get the name of the directory for the news source with raw data
FROMDIR="/Users/username/${REPONAME}/${FROM}"
TODIR="${BOXDATA}/${TO}"
COMMAND="/usr/bin/rsync -hvrPt ${FROMDIR} ${TODIR}"
echo "---------------------------------------------------------------" >> $BASHLOG
echo $COMMAND >> $BASHLOG
eval $COMMAND >> $BASHLOG
done
I put this file in ~/Library/LaunchAgents
, and loaded it using launchctl load -w [PLISTFILENAME]
.
This is the error I get in the output syncdata.err
file when the launchd job tries to run at 11:15am:
/bin/zsh: can't open input file: /Users/username/Desktop/repos/china/zzz_pipelines/sync_data.sh
How can I give the launchd job permission to run my zsh script? These are the permissions on the script when I run ls -lh
:
-rwxr-xr-x 1 username 1534107694 3.2K Feb 15 10:17 sync_data.sh
It has execute permissions for me as the user, and my understanding is that launchd scripts run as the user, so I don't understand why it's not working. Any help is appreciated!