Web服务需要在crash和系统启动的时候自动重启
简单的奔溃重启
while true;
do
echo 'start run'
./myapp;
sleep 3s
done
然后使用nohup运行这个脚本即可。但是这样直接运行不能保证系统重启的时候自动启动。
Upstart
在/etc/init目录下添加myapp.conf文件,内容如下
description "myapp"
author "yangjiandong"
start on startup
stop on shutdown
respawn
console output
script
logger "start"
exec /home/***/myapp
end script
然后使用'start myapp'命令启动服务。
我使用的是CetOS 6操作系统,所以用Upstart,新一点的版本用Systemd,旧一点的版本用sysvinit。
Systemd
Ubuntu 15.04及以后的版本从Upstart切换到了Systemd。 新建文件/etc/systemd/system/myprocess.service,内容如下:
[Unit]
Description=My Process
[Service]
ExecStart=/bin/myprocess
Restart=always
[Install]
WantedBy=multi-user.target
启动服务:
systemctl enable myprocess.service
systemctl start myprocess.service
systemctl status myprocess.service
注意事项 ExecStart指向的脚步要确保有#!/bin/sh类似的标记。