解决了!在我的所有脚本的顶部添加#!/ bin / bash以便使用bash扩展。否则它将自己限制为POSIX shell语法。谢谢Barmar!
另外,我补充一点,我执行完gpg解密后无法使用cronjob解决问题,答案是在gpg命令中添加--no-tty选项(无终端输出)
我对linux很新,所以请耐心等待......
当以ubuntu身份登录时,我可以用crontab -e执行一个简单的脚本:
* * * * * /ngage/extract/bin/echoer.sh
并且此bash脚本只是将输出打印到文件:
echo "Hello" >> output.txt
但是当我尝试以完全相同的方式执行更复杂的bash脚本时,它不起作用:
* * * * * /ngage/extract/bin/superMasterExtract.sh
此脚本调用其他bash脚本。总共有4个脚本,其中3个层次结构。它是superMasterExtract> masterExtract> (解密,解压缩)
以下是superMasterExtract.sh(顶级)的代码:
shopt -s nullglob # ignore empty file
cd /str/ftp
DIRECTORY='writeable'
for d in */ ; do # for all directories in /str/ftp
if [ -d "$d$DIRECTORY" ]; then # if the directory contains a folder called 'writeable'
files=($d$DIRECTORY/*)
dirs=($d$DIRECTORY/*/)
numdirs=${#dirs[@]}
numFiles=${#files[@]}
((numFiles-=$numdirs))
if [ $numFiles -gt 0 ]; then # if the folder has at least one file in it
bash /ngage/extract/bin/masterExtract.sh /str/ftp ${d:0:${#d} - 1} # execute this masterExtract bash script with two parameters passed in
fi
fi
done
masterExtract.sh:
DATE="$(date +"%m-%d-%Y_%T")"
LOG_FILENAME="log$DATE"
LOG_FILEPATH="/ngage/extract/logs/$2/$LOG_FILENAME"
echo "Log file is $LOG_FILEPATH"
bash /ngage/extract/bin/decrypt.sh $1 $2 $DATE
java -jar /ngage/extract/bin/sftp.jar $1 $2
bash /ngage/extract/bin/unzip.sh $1 $2 $DATE
java -jar /ngage/extract/bin/sftp.jar $1 $2
echo "Log file is $LOG_FILEPATH"
decrypt.sh:
shopt -s nullglob
UPLOAD_FILEPATH="$1/$2/writeable"
DECRYPT_FOLDER="$1/decryptedFiles/$2"
HISTORY_FOLDER="$1/encryptHistory/$2"
DONE_FOLDER="$1/doneFiles/$2"
LOG_FILENAME="log$3"
LOG_FILEPATH="/ngage/extract/logs/$2/$LOG_FILENAME"
echo "DECRYPT_FOLDER=$DECRYPT_FOLDER" >> $LOG_FILEPATH
echo "HISTORY_FOLDER=$HISTORY_FOLDER" >> $LOG_FILEPATH
cd $UPLOAD_FILEPATH
for FILE in *.gpg;
do
FILENAME=${FILE%.gpg}
echo ".done FILE NAME=$UPLOAD_FILEPATH/$FILENAME.done" >> $LOG_FILEPATH
if [[ -f $FILENAME.done ]]; then
echo "DECRYPTING FILE=$UPLOAD_FILEPATH/$FILE INTO $DECRYPT_FOLDER/$FILENAME" >> $LOG_FILEPATH
cat /ngage/extract/.sftpPasswd | gpg --passphrase-fd 0 --output "$DECRYPT_FOLDER/$FILENAME" --decrypt "$FILE"
mv $FILE $HISTORY_FOLDER/$FILE
echo "MOVING FILE=$UPLOAD_FILEPATH/$FILE INTO $HISTORY_FOLDER/$FILE" >> $LOG_FILEPATH
else
echo "Done file not found!" >> $LOG_FILEPATH
fi
done
cd $DECRYPT_FOLDER
for FILE in *
do
mv $FILE $DONE_FOLDER/$FILE
echo "DECRYPTED FILE=$DONE_FOLDER/$FILE" >> $LOG_FILEPATH
done
如果有人知道为什么它拒绝执行我更复杂的剧本,我很乐意听到它。我也尝试在crontab的开头设置一些环境变量:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/local/bin:/usr/bin
MAILTO=jgardnerx85@gmail.com
HOME=/
* * * * * /ngage/extract/bin/superMasterExtract.sh
注意,我不知道这些是我的安装或脚本的适当变量。我只是把它们从其他帖子中拉出来并尝试无济于事。如果这些不是正确的环境变量,有人可以告诉我如何为我的特定应用推断出正确的环境变量吗?
答案 0 :(得分:0)
您需要使用
开始编写脚本#!/bin/bash
以便使用bash
扩展名。否则它将自己限制为POSIX shell语法。