1. 程式人生 > >linux監測程式異常退出後重新啟動以及linux開機自啟動

linux監測程式異常退出後重新啟動以及linux開機自啟動

利用shell指令碼,輪詢監控應用的程序,如果出現崩潰程序不在的情況下,自動啟動應用程式。
1、建立一個shell指令碼:

touch monitorRestart.sh;

2、修改指令碼許可權:

chmod 777 monitorRestart.sh;

3、編輯檔案:

vim monitorRestart.sh;

檔案內容:

#!/bin/sh
while true
do
	ps -ef | grep "使用者程式名" | grep -v "grep"
if [ "$?" -eq 1 ]
then
	./run.sh #啟動應用,修改成使用者啟動應用指令碼或命令
	echo "process has been restarted!"
else echo "process already started!" fi sleep 10 done

4、啟動指令碼

./monitorRestart.sh

5、將監控指令碼monitorRestart.sh加入到Linux開機自啟動中:
方法一:
/etc/rc.local中新增一段程式即可;
在/etc/rc.local的末尾新增一行以絕對路徑啟動指令碼的行:

 vim /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in
here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local . /etc/rc.d/rc.tune /opt/bbj/bbjtest.sh

儲存並退出;
再重啟動測試下,則在其它的程式都啟動完成後,將啟動指令碼;

方法二:
init.d目錄下都為可執行程式,他們其實是服務指令碼,按照一定格式編寫,Linux 在啟動時會自動執行,類似Windows下的服務。
vi /etc/rc.d/init.d/myselfstart,追加如下內容:

#!/bin/bash
#chkconfig:2345
80 05 --指定在哪幾個級別執行,0一般指關機,6指的是重啟,其他為正常啟動。80為啟動的優先順序,05為關閉的優先機 #description:myselfstart service RETVAL=0 start(){ --啟動服務的入口函式 echo -n "myselfstart serive ..." cd /home/sigproc su sigproc -c "111111 /home/sigpro/bbj.sh" } stop(){ --關閉服務的入口函式 echo "myselfstart service is stoped..." } case $1 in --使用case,可以進行互動式操作 start) start ;; stop) stop ;; esac exit $RETVAL

執行chmod +r /etc/rc.d/init.d/myselfstart,使之可直接執行;
執行chkconfig --add myselfstart,把該服務新增到配置當中;
執行chkconfig --list mystart,可以檢視該服務程序的狀態;
重啟後檢視ps -ef |grep “bbj.sh”;