服務器上運行了一些小工具屬於非服務類的,程序進程本身不是很穩定總是異常關閉寫個腳本監視它,判斷進程是否存在並啓動該進程。
Linux判斷進程是否存在並啓動該進程
#!/bin/bash#判斷進程是否存在,如果不存在就啓動它
PIDS=`ps -ef |grep myprocess |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then 或 if [ $PIDS ]; then
echo "myprocess is runing!"
else
cd /root/
./myprocess#運行進程
fi
linux判斷進程是否存在並重啓該進程
#!/bin/bash#判斷進程是否存在,如果不存在就啓動它如果存在就重啓它
PIDS=`ps -ef |grep myprocess |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then 或 if [ $PIDS ]; then 相反判斷 if [ ! $PIDS ]; then 不存在$PIDS
kill -9 $PIDS#先關閉進程,在運行此進程
cd /root/myprocess
sudo ./myprocess#重新運行進程
else
cd /root/myprocess
sudo ./myprocess#運行進程
fi
最後編輯crontab -e 按需要設置運行時間。