#!/bin/bash
### BEGIN INIT INFO
# Provides: appname
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: appinfo
# Description: appinfo
### END INIT INFO
bin="/home/user/app"
start(){
ps -ef | grep $bin | grep -v grep > /dev/null
if [ $? -eq 0 ]; then
echo "The Service has started."
else
echo -n "Starting..."
$bin 1>/dev/null 2>/dev/null &
echo "Done!"
fi
}
stop(){
ps -ef | grep $bin | grep -v grep > /dev/null
if [ $? -eq 0 ]; then
echo -n "Stoping..."
killall $bin
echo "Done!"
else
echo "The service did not start."
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
ps -ef | grep $bin | grep -v grep > /dev/null
if [ $? -eq 0 ]; then
echo "The service is running."
else
echo "The service is not running."
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
;;
esac
脚本放在 /etc/init.d/
目录下,添加 执行 权限。
# 添加到服务
sudo systemctl enable app # app 是服务文件名
# 启动服务
sudo service app start
继续阅读 →