Linux应用保活脚本
常用的标准Linux环境里都有Systemd提供后台服务进程管理及保活,但是在一些特殊的运行环境还是需要简易的解决方案。
比如在serv00服务器freeBSD Linux环境下,可以写这个这样的脚本:
check_and_start_process.sh
#!/bin/bash
# Define process name and start command
PROCESS_NAME="command"
START_COMMAND="/path/to/your/command"
# Check if the process is running
if ! pgrep -f "$PROCESS_NAME" > /dev/null
then
echo "Process $PROCESS_NAME is not running, starting it now..."
# Start the process
$START_COMMAND &
if [ $? -eq 0 ]; then
echo "Process $PROCESS_NAME started successfully."
else
echo "Failed to start process $PROCESS_NAME."
fi
else
echo "Process $PROCESS_NAME is already running."
fi
修改脚本中的PROCESS_NAME
和START_COMMAND
值,并增加运行权限:
chmod +x check_and_start_process.sh
接着添加定时任务:
crontab -e
每5分钟检测一次:
*/5 * * * * /path/to/check_and_start_process.sh
提示
在serv00服务器中,定时任务执行后如果会触发email,则改用:
*/5 * * * * /path/to/check_and_start_process.sh > /dev/null 2>&1