常用Linux开机启动添加方法

Ubuntu添加开机启动

两步添加开机启动:

1.编写开机脚本如下:

这个脚本需要放在/etc/init.d/下面

#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/test
NAME=test
DESC=test
PID=/var/run/test/test.pid

test -x $DAEMON || exit 0

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile $PID \
                --name $NAME --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon -K --quiet --pidfile $PID \
                --name $NAME
	killall -9 dc
        echo "$NAME."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon -K --quiet --pidfile $PID \
                --name $NAME
        sleep 1
        start-stop-daemon --start --quiet --pidfile $PID \
                --name $NAME --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile $PID \
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

2.添加到开机启动

update-rc.d test defaults 91

test 就是第一步中编写的脚本名称,在/etc/ini.d目录下面。
通过这条命令,就可以把该应用添加到开机启动了。

Centos添加开机启动

与Ubuntu方法类似, 也需要两步,只是脚本和命令有点区别:

1.脚本

这个脚本依然是放在/etc/init.d/下面,但是多了两行如下参数:
#chkconfig: 2345 60 82: chkconfig后面有三个参数2345,60和82告诉chkconfig程序,需要在/etc/rc.d/rc2.d~rc5.d目录下,创建名字为 S60teststart的文件连接,连接到/etc/rc.d/init.d目录下的的teststart脚本。第一个字符是S,系统在启动的时候,运行脚本teststart,就会添加一个start参数,告诉脚本,现在是启动模式。
同时在/etc/rc.d/rc0.d和/etc/rc.d/rc6.d目录下,创建名字为K82teststart的 文件连接,第一个字符为K,在关闭系统的时候,会运行teststart,添加一个stop参数,告诉脚本,现在是关闭模式。

#! /bin/sh
#chkconfig:  2345 60 82
#description: Starttest

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/test
NAME=test
DESC=test
PID=/var/run/test/test.pid

test -x $DAEMON || exit 0

set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile $PID \
                --name $NAME --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon -K --quiet --pidfile $PID \
                --name $NAME
	killall -9 dc
        echo "$NAME."
        ;;
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon -K --quiet --pidfile $PID \
                --name $NAME
        sleep 1
        start-stop-daemon --start --quiet --pidfile $PID \
                --name $NAME --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile $PID \
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

2.添加到开机启动

chkconfig --add test #添加启动
chkconfig #查看当前启动列表


注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'test           	0:关	1:关	2:开	3:开	4:开	5:开	6:关
mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

欢迎关注【fensnote】

猜你喜欢

转载自blog.csdn.net/wuquan_1230/article/details/128343587