2013-10-14 81 views
0

我有一個服務器應用程序,其中包含1500個進程ID,我需要一個shell腳本,它檢查每個小時是否進程已啓動,如果不啓動進程,使用「dsmc -u xxxx -p * *「如果沒有開始發送郵件到我的Gmail([email protected])。這裏是代碼如何發送郵件到我的Gmail帳戶。Linux腳本來檢查進程是否已啓動

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" [email protected] 
    fi 

我沒有收到郵件到我的Gmail帳戶。 參考:http://rtcamp.com/wordpress-nginx/tutorials/linux/ubuntu-postfix-gmail-smtp/

回答

0

寫腳本,你可能會想探討以下功能及其相關手冊頁

PS p纖ep ptree中 郵件

0

腳本的一部分是很容易做到。但是,郵件疑難解答不是。我建議在屏幕上運行腳本(screen -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" [email protected] 
    else 
    echo "The FTP server could not restart" | mail -s "Ftp Server $process is down" [email protected] 
    sleep 1 
    exit 0; 
    fi 

done 

希望這會有所幫助,祝你好運。

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" [email protected] 
else 
sleep 0 
fi 

的cron:

* * * * * /bin/sh /path/to/vsftpd.sh > /dev/null 2>&1 
相關問題