简体   繁体   中英

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

I have written a bash script to e-mail a log file. This script works properly when executed at the command line of the Linux server. However, when I schedule the script using crontab I get this error message:

cat: : No such file or directory

Here is the crontab line calling the script:

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

Here is the script itself:

#!/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

The crontab executes the script, and sends me a blank e-mail (no log file in attachment or body).

Here is the error message found in the crontab log file after the cron job executes:

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**

Any insight you could provide would be greatly appreciated. Thank You.

Thank You for the kind advice @Gordon Davisson and @Nic3500. I managed to hack away at the code untile the error message changed to "File not found". That tipped me off to look into the scp portion of the script. The "iteract" statement at the end was inappropriate for an automated cron job. Changing it from "interact" to "expect eof" solved the problem.

Thanks for a great first experience on StackOverflow!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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