我的 bash 脚本在由 crontab 触发时不起作用,但当我手动触发它时可以工作?

我的 bash 脚本在由 crontab 触发时不起作用,但当我手动触发它时可以工作?

这是 cron 条目

*/1 * * * * ./home/usr/automation/check_rejected_files.sh > /home/usr/automation/cronout

它生成了文件但没有其他事情发生,我在脚本的开头添加了一个日期日志,这样我就可以看到脚本是否运行,并且当它通过 cron 触发时不会发生任何日志记录,感谢您的帮助,谢谢

脚本

#! /bin/bash
#
#
now=$(date)
echo $now >> $ASfile

*emphasized text*
##########o 
#Variables #
############

ASfile='AlertsSent.txt'
Rlines=$(cat $ASfile)
Alerted="false"
Recipient="mymail"

USERNAME='usr'
PASSWORD='pass'
SERVICE_NAME='service'
#####################
#Query List from DB #
#####################

RejectionQueryOutput=$(sqlplus -s $USERNAME/$PASSWORD@$SERVICE_NAME <<END
        set showmode off;
        set trimout on;
        set feedback off;
        set  verify off;
        set  heading off;
        set  echo off;
        set pagesize 0;
        set termout off;
    select mmfile.name from file_status INNER JOIN mmfile ON file_status.file_id=mmfile.id where file_status.state='BANK_REJECTED' and file_status.type='CURRENT' and file_status.datetime > sysdate - 6 and (mmfile.type='PAIN008_EPC' or mmfile.type='PAIN008_CGI' or mmfile.type='PAIN008_CBI' or mmfile.type='PAIN008_DIAS' or mmfile.type='PAIN001_EPC' or mmfile.type='PAIN001_CGI');
    exit;
END
)

ExportedQueryOutput=$(sqlplus -s $USERNAME/$PASSWORD@$SERVICE_NAME <<END
        set showmode off;
        set trimout on;
        set feedback off;
        set  verify off;
        set  heading off;
        set  echo off;
        set pagesize 0;
        set termout off;
     select mmfile.name from file_status INNER JOIN mmfile ON file_status.file_id=mmfile.id where file_status.state='EXPORTED' and file_status.type='CURRENT' and file_status.datetime < sysdate - 2/24 and file_status.datetime > sysdate - 6 and (mmfile.type='PAIN008_EPC' or mmfile.type='PAIN008_CGI' or mmfile.type='PAIN008_CBI' or mmfile.type='PAIN008_DIAS' or mmfile.type='PAIN001_EPC' or mmfile.type='PAIN001_CGI');
    exit;
END
)
tifs=$IFS
IFS=' '
RejectedArray=( $(for i in $RejectionQueryOutput ; do echo $i ; done) )
ExportedArray=( $(for i in $ExportedQueryOutput ; do echo $i ; done) )
IFS=$tifs
#######
#Main #
#######
#Rejected 
for i in ${RejectedArray[@]}
do
        for rline in $Rlines; do
                if  [ "$i" == "$rline" ]
                then
                        Alerted="true"
                        break
                fi
        done

        if [ "$Alerted" == "false" ]
        then
                echo $i "Rejected"  >> $ASfile
                echo "file $i have been rejected by bank please take action" | mail -s "[SEPA] raising alarm: $i have been rejected" $Recipient
        fi
Alerted="false"
done
ASfile='AlertsSent.txt'
Elines=$(cat $ASfile)
#Stuck Exporting
for x in ${ExportedArray[@]}
do
        for eline in $Elines; do
                if  [ "$x" == "$eline" ]
                then
                        Alerted="true"
                        break
                fi
        done

        if [ "$Alerted" == "false" ]
        then
                echo $x "Long-Exporting"  >> $ASfile
                echo "file $x have been in Exported state for more than 2 hours" | mail -s "[SEPA] raising alarm: $x have been in Exporting state for more than 2 hours" $Recipient
        fi
Alerted="false"
done

答案1

crontab像这样删除条目中的点:

*/1 * * * * /home/usr/automation/check_rejected_files.sh > /home/usr/automation/cronout

相关内容