Examples of using shells to process and send mail

Implementing functions

1. Obsolete processing

Call and execute php in a shell script. If php cannot be completed within 300 seconds, The execution ends

2. Email sending

Send an email when an error is reported, and the content of the email contains the content of the error log output when php is executed

#!/bin/bash
TIMEOUT=300
FROM_ADDR="[email protected]"
TO_ADDRS=(
"[email Protected]"
)
CC_ADDRS=(

)
MAIL_CONTENT_FILE="/var/www/project/fuel/app/tasks/mailcontent.php"

function sync(){
php_command="php /var/www/project/oil refine sync diff"
res=$($php_command)
if (( $?!=0 ))
then
sendmail ""
return 0
fi
if [[ -n $res ]]
then
sendmail ""
return 0
fi
}

function sendmail(){
if [$BORG_FUEL_BATCH_ENV = development]
then
subject=‘[borg ph0][DEV] failed to create the same period ファイル’
elif [$BORG_FUEL_BATCH_ENV = staging]
then
subject=‘[borg ph0][STG] failed to create the same period ファイル’
else
subject=‘[borg ph0][PRD] failed to create the same period ph0’
fi

from=$FROM_ADDR
to="${TO_ADDRS[@]}"
cc=""
for cc_mail in ${CC_ADDRS[@]}
do
cc="$cc -c $cc_mail "
done

log_file="/var/www/borg/fuel/app/tasks/logs/$(date "+%Y")/$(date "+%m")/$(date "+%d").log"
title="At the same time ファイル作成处理はエラーになりました."
grep_msg="ERROR-$(date "+%Y-%m-%d %H:%M")"
echo $title> $MAIL_CONTENT_FILE
cat $log_file | grep "$grep_msg" >> $MAIL_CONTENT_FILE
echo $1 >> $MAIL_CONTENT_FILE

cat "$MAIL_CONTENT_FILE" | mail -s "$subject" -r $from $cc $to
if(( $?!=0 ))
then
echo "Notify メールは delivery failed."
fi
}

function timeout_custom()
{
        waitfor=$TIMEOUT
        command=$*
        $command &
        commandpid=$!

        (sleep $waitfor; sendmail "Synchronous ファイル creation process ししました for more than ${waitfor} seconds."; kill -9 $commandpid >/dev/null 2>&1 )&
        sleeppid=$!
        wait $commandpid >/dev/null 2>&1
        kill $sleeppid >/dev/null 2>&1
}

timeout_custom sync

#!/bin/bash
TIMEOUT=300
FROM_ADDR="[email protected]"
TO_ADDRS=(
"[email Protected]"
)
CC_ADDRS=(

)
MAIL_CONTENT_FILE="/var/www/project/fuel/app/tasks/mailcontent.php"

function sync(){
php_command="php /var/www/project/oil refine sync diff"
res=$($php_command)
if (( $?!=0 ))
then
sendmail ""
return 0
fi
if [[ -n $res ]]
then
sendmail ""
return 0
fi
}

function sendmail(){
if [$BORG_FUEL_BATCH_ENV = development]
then
subject=‘[borg ph0][DEV] failed to create the same period ファイル’
elif [$BORG_FUEL_BATCH_ENV = staging]
then
subject=‘[borg ph0][STG] failed to create the same period ファイル’
else
subject=‘[borg ph0][PRD] failed to create the same period ph0’
fi

from=$FROM_ADDR
to="${TO_ADDRS[@]}"
cc=""
for cc_mail in ${CC_ADDRS[@]}
do
cc="$cc -c $cc_mail "
done

log_file="/var/www/borg/fuel/app/tasks/logs/$(date "+%Y")/$(date "+%m")/$(date "+%d").log"
title="At the same time ファイル作成处理はエラーになりました."
grep_msg="ERROR-$(date "+%Y-%m-%d %H:%M")"
echo $title> $MAIL_CONTENT_FILE
cat $log_file | grep "$grep_msg" >> $MAIL_CONTENT_FILE
echo $1 >> $MAIL_CONTENT_FILE

cat "$MAIL_CONTENT_FILE" | mail -s "$subject" -r $from $cc $to
if(( $?!=0 ))
then
echo "Notify メールは delivery failed."
fi
}

function timeout_custom()
{
        waitfor=$TIMEOUT
        command=$*
        $command &
        commandpid=$!

        (sleep $waitfor; sendmail "Synchronous ファイル creation process ししました for more than ${waitfor} seconds."; kill -9 $commandpid >/dev/null 2>&1 )&
        sleeppid=$!
        wait $commandpid >/dev/null 2>&1
        kill $sleeppid >/dev/null 2>&1
}

timeout_custom sync

Leave a Comment

Your email address will not be published.