用于检查进程的Linux脚本是否已启动

时间:2013-10-14 10:01:42

标签: linux shell email smtp

我有一个服务器应用程序,它拥有1500个进程ID,我需要一个shell脚本,每小时检查一次进程是否已启动,如果没有使用“dsmc -u xxxx -p * *“如果没有开始发送邮件到我的Gmail(xxx@gmail.com)。这是代码如何发送邮件到我的Gmail帐户。

   if pidof -s vsftpd = /dev/null; then
      echo 'ftp is stopped'

       sudo /etc/init.d/vsftpd restart
  else 
       echo "The FTP server is Down" | mail -s "Ftp Server is Down" abcd@example.com
   fi

我没有收到邮件到我的Gmail帐户。 参考:http://rtcamp.com/wordpress-nginx/tutorials/linux/ubuntu-postfix-gmail-smtp/

3 个答案:

答案 0 :(得分:0)

要编写脚本,您可能希望调查以下函数及其相关联的手册页

PS p纤ep ptree中 邮件

答案 1 :(得分:0)

脚本部分很容易做到。但邮件故障排除则不然。我建议在屏幕上运行脚本(屏幕-d -m / bin / bash“test.sh”),分离屏幕(ctrl -a + d)然后终止进程(/etc/init.d/vsftp stop) ,等待1分钟,然后重新连接屏幕(屏幕-r)。这将为您提供可以排除故障的邮件错误。

以下脚本将为您监控您的服务。

    #!/bin/bash

process="vsftp"

while true ; do

  until [ ! $(pgrep $process) ]; do
        sleep 1 #The number or minutes to wait until next check
  done

  #If process is not found do the following
  /etc/init.d/$process start > /dev/null #Run as root because sudo requires password
  if [ $? != 1 ]; then
  echo "The FTP server was restarted" | mail -s "Ftp Server $process was restarted" abcd@example.com
  else
  echo "The FTP server could not restart" | mail -s "Ftp Server $process is down" abcd@example.com
  sleep 1
  exit 0;
  fi

done

希望这会有所帮助并祝你好运。

答案 2 :(得分:0)

我们可以使用ps -ef

来编写脚本

试,

 # cat vsftpd.sh
 #!/bin/bash
 /bin/ps -ef | grep vsftpd > /dev/null 2>&1
 if [ $? -ne 0 ]
 then
 /etc/init.d/vsftpd restart > /dev/null 2>&1
 /bin/mail -s "FTP service is RESTARTED now" abcd@example.com
 else
 sleep 0
 fi

的cron:

 * * * * * /bin/sh /path/to/vsftpd.sh > /dev/null 2>&1