我有一个shell脚本。我在这个脚本中传递一个文件中的参数。该文件包含表名
脚本运行正常。我能够对文件中的所有表执行命令。
shell script
#!/bin/bash
[ $# -ne 1 ] && { echo "Usage : $0 input file "; exit 1; }
input_file=$1
TIMESTAMP=`date "+%Y-%m-%d"`
touch /home/$USER/logs/${TIMESTAMP}.success_log
touch /home/$USER/logs/${TIMESTAMP}.fail_log
success_logs=/home/$USER/logs/${TIMESTAMP}.success_log
failed_logs=/home/$USER/logs/${TIMESTAMP}.fail_log
#Function to get the status of the job creation
function log_status
{
status=$1
message=$2
if [ "$status" -ne 0 ]; then
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
#echo "Please find the attached log file for more details"
#exit 1
else
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
fi
}
while read table ;do
sqoop job --exec $table > /home/$USER/logging/"${table}_log" 2>&1
g_STATUS=$?
log_status $g_STATUS "Sqoop job ${table}"
cp /home/$USER/logging/"${table}_log" /home/$USER/debug/`date "+%Y-%m-%d"`/logs/
done < ${input_file}
现在,我想将电子邮件发送到failed jobs
的电子邮件地址。
Requirements
1) Send email for each failed job i.e If `status log` has failed job for one particular table then I want email sent out saying job for that table has failed.
2) Or Send out one email for all the jobs that have failed for one input file.
哪种方法最好。我希望2nd
选项至少可以减少通过电子邮件的数量。
但如果我知道两种方法都可以做得更好
edited function log_status
#Function to get the status of the job creation
function log_status
{
status=$1
message=$2
if [ "$status" -ne 0 ]; then
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
mail -a mail.txt -s "This is the failed job log" user@example.com < /home/$USER/logs/${TIMESTAMP}.fail_log
#exit 1
else
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
fi
}
如果我这样做,我会收到一封电子邮件,说明所有失败的工作。
答案 0 :(得分:2)
也可以使用sendmail
命令:
sendmail user@example.com < email.txt
使用mail
命令:
mail -a mail.txt -s "This is the failed job log" user@example.com
-a
是附件,-s
是主题
有许多附件:
$(uuencode file1.txt file2.txt) | mailx -s "Subject" user@example.com
答案 1 :(得分:0)
以下是发送err.log的简单代码段:
mail -s "stder logs for ps" "name@example.com" < err.log
这会在第一个引号中附加一个主题,并将其邮寄给第二个集合中的收件人。在这种情况下,我想你会有一个代码块通过3>
将错误日志导出到我称为err.log
的文件中以便于阅读。如果你不想担心对错误过于直言,或者如果你想更频繁地通过电子邮件发送给收件人,那么我会把上面的代码段放在你的脚本中。