简单shell脚本实现守护进程

以守护haproxy进程为例,监控周期为600秒(10分钟)。

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh
while true;do
ps -fe|grep haproxy |grep -v grep
if [ $? -ne 0 ]
then
echo "Start process....."
service haproxy-lkl start
else
echo "Running now....."
fi
sleep 600
done

保存为monitor.sh后增加执行权限:

1
$ chmod +x monitor.sh

⚠️ 如果不修改权限,则需通过bash命令运行脚本。

后台运行,保证脚本在退出终端后继续运行:

1
$ nohup ./monitor.sh &

Read More