Сценарий crontab, вызывающий mailx, не работает, но тот же скрипт работает в командной строке - PullRequest
0 голосов
/ 25 марта 2020

Я написал скрипт 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 выполняет скрипт и отправляет мне пустое электронное письмо (без файла журнала во вложении или теле).

Вот сообщение об ошибке, найденное в файле журнала crontab после выполнения задания cron:

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

Буду признателен за любые идеи, которые вы сможете предоставить. Спасибо.

1 Ответ

0 голосов
/ 27 марта 2020

Спасибо за добрый совет @ Гордон Дэвиссон и @ Nic3500. Мне удалось взломать код, пока сообщение об ошибке не изменилось на «Файл не найден». Это помогло мне разобраться в части сценария scp. Оператор «iteract» в конце не подходит для автоматизированной работы cron. Изменение его с «взаимодействовать» на «ожидать чего-то» решило проблему.

Спасибо за отличный первый опыт работы со StackOverflow!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...