http://blog.csdn.net/special23/article/details/52690057
Service-side programs generally need long-term background stable operation, in addition to ensuring the robustness of the program itself, but also need to use some daemons.
Now share a simple shell script in the way:
#! /bin/sh
#进程名字可修改
pro_name=rails
cmd= "nohup bundle exec Rails Server Webrick-p3000-b 0.0.0.0-e-production & Gt;/dev/null 2>&1-d & "While
true: do
#用ps获取 $PRO number of _name processes
num= ' ps aux | grep-w ${pro_name} | gre p-v grep |wc-l '
#echo $NUM
#少于1, restart process
if ["${num}"-lt "1"];then
echo "${pro_name} was killed"
$CM D
#大于1, kill all Processes, reboot
elif ["${num}"-gt "1"];then
echo "More than 1 ${pro_name},killall ${pro_name}"
Killall-9 $PRO _name
$CMD
fi
#kill僵尸进程
num_stat= ' ps aux | grep-w ${pro_name} | grep T | grep-v grep | Wc-l '
if ["${num_stat}"-gt "0"];then
killall-9 ${pro_name}
$CMD
fi sleep
5s
done
Exit 0
Script Saved as xxx.sh
Set script execution permissions chmod 777 xxx.sh
Console execution:./xxx.sh This ensures that there is always a XXXX program running.
Need to keep running in the background: nohup ./xxx.sh &