服务器修补电子邮件通知linux脚本

时间:2016-03-16 07:34:40

标签: linux bash shell

我需要创建一个可以在服务器修补活动中使用的电子邮件通知linux脚本。我为同一个创建了一个脚本,但是遇到了一些错误。任何人都可以检查问题是什么。 脚本 -

#email constants
export to_addresses=xyz@abc.com

auth_1_app_path=/export/home/apps/test1/dev1;

function sendServerPatchingSuccessMail()
{
printf  "HI Team,\r\n\r\nServer patching activity has been done    successfully.\r\n\r\nRequest eText DEV team to validate the server patching changes.\r\n\r\nTotal time taken = $(( ($end_date-$start_date) / ( 60) )) minutes.\r\n\r\nRegards,\r\nAdmin\r\n\r\nNote: This is an auto generated mail, please do not reply. " | mail -s  "server patching activity is done sucessfully." $to_addresses -c $cc_addresses 
}

function sendServerPatchingStartMail()
{
printf  "Hi Team,\r\n\r\nServer patching activity has started at $start_date.\r\n\r\nRequest eText DEV team to validate the server patching changes once they receive the server patching success mail.\r\n\r\nRegards,\r\nAdmin\r\n\r\nNote: This is an auto generated mail, please do not reply. " | mail -s  "server patching activity has started." $to_addresses -c $cc_addresses 

}

#define list of hosts as array
servers_array=( bookvm04 #DEV1 
       );

function doWork() { 

start_date=$(date +%Y%m%d%H%M%S);
sendServerPatchingStartMail

 #stop tomcat on all hosts   
 echo "`date "+%Y-%m-%d %H:%M:%S"`  Stop tomcat stop."
#start for each host
for i in "${servers_array[@]}"
do
export ssh_to_remote_host="ssh -l bookweb -i /export/home/apps/bookplus/.ssh/id_dsa "

case $i in


 #DEV1 Book Server
 b3bookvm05)
    app_path=$view_1_app_path;
    ;;
#DEV1 Book Server
b3bookvm04)
    app_path=$auth_1_app_path;

    ;;

*)   echo "`date "+%Y-%m-%d %H:%M:%S"`  $i is not a valid option exiting"
exit 0;;
esac #end case

$ssh_to_remote_host $i $app_path/scripts/stopremote


if [ "$?" -ne 0 ]
then
echo "`date "+%Y-%m-%d %H:%M:%S"`  node ${i} could not be stopped"
exit 0
fi



end_date=$(date +%Y%m%d%H%M%S); 
echo "`date "+%Y-%m-%d %H:%M:%S"`  Total time taken = $(( ($end_date-        $start_date) / ( 60) )) minutes";
sendServerPatchingSuccessMail
}

执行上述脚本时出错

-bash-4.1$ sh server_patching_alert.sh
: command not foundrt.sh: line 4:
: command not foundrt.sh: line 5:
: command not foundrt.sh: line 9:
: command not foundrt.sh: line 13:
'erver_patching_alert.sh: line 14: syntax error near unexpected token `
'erver_patching_alert.sh: line 14: `function sendServerPatchingSuccessMail()

1 个答案:

答案 0 :(得分:-1)

这个脚本运行正常。

server_array=$(hostname)

function sendServerPatchingSuccessMail()
{

# Set email constants
to_addresses=
cc_addresses=

# Send server patching completion mail

printf  "mail body - server patching activity for hostname '$server_array' has been completed successfully" | mail -s  "mail subject" -c $cc_addresses $to_addresses
}

 sendServerPatchingSuccessMail