繁体   English   中英

crontab 脚本调用 mailx 失败,但相同的脚本在命令行上工作

[英]crontab script calling mailx fails, but same script works on command line

我编写了一个 bash 脚本来通过电子邮件发送日志文件。 此脚本在 Linux 服务器的命令行上执行时可以正常工作。 但是,当我使用 crontab 安排脚本时,我收到以下错误消息:

cat:: : 没有那个文件或目录

这是调用脚本的 crontab 行:

30 10 * * * /home/oracle/app/oracle/script/db2_prod/etl_log_emailer_1.3.sh >> /home/oracle/app/oracle/script/db2_prod/etl_log_emailer_crontab.log 2>&1

这是脚本本身:

#!/bin/bash
__='
Name    : etl_log_emailer.sh
Purpose : Copy the ETL log file from db3 to rmancat, and then e-mail it.
'
### Start Best Practices
# Fail on uninitialized variables rather than treating them as null.
set -u
# Fail on the first program that returns $? != 0
set -e
# Fail an entire pipeline if any element of the pipe has failed.
set -o pipefail
# Include filename and line number in the debug prompt.
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
### End Best Practices
#
# Set working directory.
#
DIR=/home/oracle/app/oracle/script/db2_prod/etl_log
#
# Cleanup any leftover files on disk from the previous run.
#
find /home/oracle/app/oracle/script/db2_prod/etl_log -type f -name "Batch*" -delete
#
# Load the password of the login on remote server db3.
#
. /home/oracle/app/oracle/script/db2_prod/login_password.sh
#
# Secure copy (scp) the ETL log files from server db3 to server rmancat.
#
/usr/bin/expect -c 'spawn -noecho scp login@db3:/batch_logs/Batch* /home/oracle/app/oracle/script/db2_prod/etl_log/ ; expect "assword:" ; send "'$DB2PASSWORD'\r" ; interact'
#
# Find the newest version of the log file.
#
NEWEST=$(find /home/oracle/app/oracle/script/db2_prod/etl_log -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1)
#
# E-mail the most recent ETL log file to the DBA team.
#
cat "$NEWEST" | mailx -S smtp=mailserver -s "ETL Log" -v someone@nowhere.com

crontab 执行脚本,并向我发送一封空白电子邮件(附件或正文中没有日志文件)。

以下是 cron 作业执行后在 crontab 日志文件中发现的错误消息:

login@db3's password:
**cat: : No such file or directory**
Resolving host mailserver . . . done.
Connecting to 10.0.0.1 . . . connected.
220 HOST Microsoft ESMTP MAIL Service ready at Wed, 25 Mar 2020 12:20:01 -0400
>>> HELO rmancat
250 HOST Hello [10.0.0.1]
>>> MAIL FROM:<oracle@rmancat>
250 2.1.0 Sender OK
>>> RCPT TO:<someone@nowhere.com>
250 2.1.5 Recipient OK
>>> DATA
354 Start mail input; end with <CRLF>.<CRLF>
>>> .
250 2.6.0 <5e7b84b2.oVbRP8NLMYPticZB%oracle@rmancat> [InternalId=76132590291486, Hostname=host] 1734 bytes in 0.104, 16.172 KB/sec Queued mail for delivery
>>> QUIT
221 2.0.0 Service closing transmission channel
**Null message body; hope that's ok**

您可以提供的任何见解将不胜感激。 谢谢你。

感谢@Gordon Davisson 和@Nic3500 的善意建议。 我设法破解了代码,直到错误消息更改为“找不到文件”。 这让我开始研究脚本的 scp 部分。 最后的“iteract”语句不适用于自动化的 cron 作业。 将其从“交互”更改为“期望 eof”解决了问题。

感谢您对 StackOverflow 的首次体验!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM