【问题标题】:crontab script calling mailx fails, but same script works on command line调用 mailx 的 crontab 脚本失败,但相同的脚本可在命令行上运行
【发布时间】:2020-03-25 16:33:16
【问题描述】:

我编写了一个 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**

您能提供的任何见解将不胜感激。谢谢。

【问题讨论】:

  • 将 $PATH 变量添加到脚本中并删除 $NEW 参数周围的双引号有助于抑制“cat: : No such file or directory”消息。我仍然收到一封空白的电子邮件。 crontab 日志末尾的消息显示“Null message body; hope that's ok”。不行,我还有工作要做!
  • 不要删除$NEWEST周围的双引号;删除错误消息,但不能解决实际问题。您应该在这里收到一个错误,告诉您有问题;这就是错误消息的用途。实际问题似乎是该变量为空(显然是因为find 没有找到任何匹配的文件?)。
  • 您与用户一起运行的命令会运行到您的环境中。 Crontab 在自己的环境中运行命令,与你的环境不一样。

标签: bash email cron


【解决方案1】:

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

感谢您在 StackOverflow 上的首次体验!

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 2013-01-14
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2015-09-10
    • 2011-10-22
    • 2021-05-01
    • 2013-02-06
    相关资源
    最近更新 更多