launchd 脚本无法运行我的 zsh 脚本

问题描述

我有一个 zsh 脚本,它使用 rsync文件从 EC2 实例移动到我的本地机器,然后将这些文件复制到 Box 驱动器。我有一台 Mac,所以我正在尝试使用 launchd自动化每天发生的同步。这是我的 .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.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>

这是我的 zsh 脚本,如果我将 zsh 脚本的文件路径直接粘贴到我的终端中,它运行得很好:

#!/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="[email protected]"
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

我将此文件放在 ~/Library/LaunchAgents 中,并使用 launchctl load -w [PLISTFILENAME] 加载。

这是当 launchd 作业尝试在上午 11:15 运行时我在输出 syncdata.err 文件中得到的错误

/bin/zsh: can't open input file: /Users/username/Desktop/repos/china/zzz_pipelines/sync_data.sh

如何授予 launchd 作业运行我的 zsh 脚本的权限?这些是我运行 ls -lh 时对脚本的权限:

-rwxr-xr-x   1 username  1534107694   3.2K Feb 15 10:17 sync_data.sh

它对我作为用户具有执行权限,我的理解是launchd脚本以用户身份运行,所以我不明白为什么它不起作用。任何帮助表示赞赏!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)