Ubuntu/Debian 程序自启


参考文章

  1. Debian10 开机自启动脚本
  2. debian9.6添加开机执行自己的脚本
  3. 【JokerのLinux】Debian 9 应用程序开机自启动
  4. ubuntu 下 init.d 服务启动脚本编写

一、通过/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

说明:目前只能通过上述指令启动,未实现自启,待查找原因。


总结

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值