搭建linux服務器的時候,須要寫一個簡單的守護進程來監控服務的運行狀況,shell腳本以下:linux
#!/bin/sh function daemon() { while true do server=`lsof -i:8080` #服務器佔用端口爲8080,經過查看8080端口是否佔用來判斷服務是否啓動 date=`date "+%Y-%m-%d %H:%M:%S"` if [ ! "$server" ] then echo "$date, webserver is stoped!" nohup sh startserver.sh >> nohup.out 2>&1 & #經過nohup命令後臺運行服務 echo "$date, webserver is starting..." sleep 10 #啓動後等待10s else echo "$date, webserver is running..." fi sleep 10 done } daemon
存爲monitor.sh,經過nohup ./monitor.sh >> monitor.log 2>&1 & 來啓動。web