文章目录
参考文章
一、通过/etc/rc.local启动程序
1. 创建 /etc/rc.local
在Debian 9 /etc路径下,没有rc.local这个文件,所以需要自己创建 /etc/rc.local,里面的内容是我复制的,主要就是把第一句(#!/bin/sh -e) 和 最后一句(exit 0)写进去即可。
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
## Fill in name of program here.
PROG="QTool"
PROG_PATH="/home/qudoor" ## Not need, but sometimes helpful (if $PROG resides in /opt for example).
PROG_ARGS=""
PID_PATH="/var/run/"
$PROG_PATH/$PROG $PROG_ARGS 2>&1 >/var/log/$PROG.log &
pid=`ps -ef | grep QTool | grep -v grep | awk '{print $2}'`
echo "$PROG started, pid =" $pid
#echo $pid > "$PID_PATH/$PROG.pid"
exit 0
2. 让新建文件具有x权限
sudo chmod +x /etc/rc.local
3. reboot系统
sudo reboot
4. 查看rc.local服务状态
systemctl status rc-local
失败
正常运行
二、通过/etc/init.d/* 启动程序
1.新建启动脚本
在/etc/init.d/目录下新建脚本文件,内容如下:
其中
PROG=“QTool”
PROG_PATH="/opt"
等内容,需要根据实际使用需求修改。
#!/bin/bash
### BEGIN INIT INFO
#
# Provides: QTool
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: initscript
# Description: This file should be used to construct scripts to be placed in /etc/init.d.
#
### END INIT INFO
## Fill in name of program here.
PROG="QTool"
PROG_PATH="/opt" ## Not need, but sometimes helpful (if $PROG resides in /opt for example).
PROG_ARGS=""
PID_PATH="/var/run/"
start() {
if [ -e "$PID_PATH/$PROG.pid" ]; then
## Program is running, exit with error.
echo "Error! $PROG is currently running!" 1>&2
exit 1
else
## Change from /dev/null to something like /var/log/$PROG if you want to save output.
$PROG_PATH/$PROG $PROG_ARGS 2>&1 >/var/log/$PROG &
#pid=`ps ax | grep -i 'QTool' | sed 's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`
pid=`ps -ef | grep QTool | grep -v grep | awk '{print $2}'`
echo "$PROG started, pid =" $pid
echo $pid > "$PID_PATH/$PROG.pid"
fi
}
stop() {
echo "begin stop"
if [ -e "$PID_PATH/$PROG.pid" ]; then
## Program is running, so stop it
#pid=`ps ax | grep -i 'QTool' | sed 's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`
pid=`ps -ef | grep QTool | grep -v grep | awk '{print $2}'`
kill $pid
rm -f "$PID_PATH/$PROG.pid"
echo "$PROG stopped"
else
## Program is not running, exit with error.
echo "Error! $PROG not started!" 1>&2
exit 1
fi
}
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
case "$1" in
start)
start
exit 0
;;
stop)
stop
exit 0
;;
reload|restart|force-reload)
stop
start
exit 0
;;
**)
echo "Usage: $0 {start|stop|reload}" 1>&2
exit 1
;;
esac
2. 让新建文件具有x权限
sudo chmod +x /etc/init.d/*
3.添加删除服务
添加删除服务
添加: sudo update-rc.d 服务名 defaults
删除:sudo update-rc.d -f 服务名 remove
说明:可将方法一中脚本按照此方式添加到服务中设置开机自启。
4.启动、关闭、重启服务
/etc/init.d/服务名 start
/etc/init.d/服务名 stop
/etc/init.d/服务名 start
说明:目前只能通过上述指令启动,未实现自启,待查找原因。