linux 使用shell 脚本发送邮件(使用mailx) | 您所在的位置:网站首页 › ish邮箱 › linux 使用shell 脚本发送邮件(使用mailx) |
背景 由于工作中需要使用linux监控应用程序,失败需要告警处理,在网上找了半天,发现没有一篇文章把在linux 使用shell 脚本发送邮件,于是找了很多资料,东拼西凑的,成功的使用mailx发送邮件,现总结如下 一、安装 yum install mailx -y二、配置参数 vim /etc/mail.rc set [email protected] set smtp=smtps://smtp.exmail.qq.com:465 set [email protected] set smtp-auth-password=xxxxx set smtp-auth=login set ssl-verify=ignore set nss-config-dir=/etc/pki/nssdb由于阿里云封闭了25端口,所以使用465端口发送 三、测试 echo hello word | mail -s " title" [email protected] 成功 有个小插曲由于系统的域名是data-collect-7,一直显示如下错误, 可能是不符合命名规则吧,解决方案 vim /etc/hosts 添加一行 ip data-collect-7 data.collect7参考:https://blog.51cto.com/hades02/1734336 四、解决证书问题 尽管邮件发送成功但是总是报这个错误 详细参考:https://blog.51cto.com/13162375/2173007 参考:【1】https://stackoverflow.com/questions/16799407/mailx-and-gmail-nss-config-dir 【2】https://developer.aliyun.com/ask/236538?spm=a2c6h.13524658 【3】https://blog.51cto.com/13162375/2173007 附监控任务失败发生邮件的shell脚本 #!/bin/bash while : do processExist=`ps aux|grep 'com.zendesk.maxwell.Maxwell --config conf/zebra.properties' |grep -v "grep"` #echo $processExist nowtime=$(date "+%Y-%m-%d %H:%M:%S") if [[ -z $processExist ]] ; then #if [ "$processExist" ] ; then sh xxx.sh echo "xxx任务异常,已重启,若频繁告警请登录服务器查看原因" | mail -s "data-collect-x xxx任务异常重启" [email protected] echo $nowtime "the process is restarted !" else echo $nowtime "the process is running !" fi sleep 30 done
备份 安装sendmail yum install -y sendmail systemctl start sendmail.service #启动 systemctl restart sendmail.service #重启 systemctl status sendmail.service # 状态 systemctl stop sendmail.service #停止 systemctl enable sendmail.service #服务器重启生效 |
CopyRight 2018-2019 实验室设备网 版权所有 |