Linux上的开机自启动

发布于 2020-06-02 02:16:22

写完才找到自己之前写过的一篇 SystemV, Upstart和SystemD的简单比较

历史概览

历史上有3中开启自启的相关程序,分别是 system v (update-rc.d/chkconfig(CentOS), service), upstart(initctl), sytemd(systemctl)。很多发行版提供多种选择,或允许安装备用方案。

  • System V: the older init system: /etc/init.d/, /etc/rcN.d/
    • Debian 6 and earlier
    • Ubuntu 9.04 and earlier
    • CentOS 5 and earlier
  • Upstart: /etc/init/
    • Ubuntu 9.10 to Ubuntu 14.10, including Ubuntu 14.04
    • CentOS 6
  • systemd: the init system for the most recent distributions featured here: /etc/sytem/
    • Debian 7 and Debian 8
    • Ubuntu 15.04 and newer
    • CentOS 7

实操查看有哪些目录和文件

root@tx:~# echo $SHELL
/bin/bash

root@tx:~# cat /etc/issue
Debian GNU/Linux 9 \n \l

root@tx:~# ls -lh /etc/ | grep profile
-rw-r--r-- 1 root root    767 Mar  4  2016 profile
drwxr-xr-x 2 root root   4.0K Nov 20  2019 profile.d

root@tx:~# ls -lh /etc/ | grep init
drwxr-xr-x 2 root root   4.0K Oct 13  2018 init
drwxr-xr-x 2 root root   4.0K Mar 16 12:38 init.d
drwxr-xr-x 5 root root   4.0K Jul 15  2017 initramfs-tools

root@tx:~# ls -lh /etc/ | grep rc
-rw-r--r-- 1 root root   2.0K Jul 26  2018 bash.bashrc
-rw-r--r-- 1 root root   1.8K May 16  2017 inputrc
-rw-r--r-- 1 root root   8.6K Jan 11  2017 nanorc
drwxr-xr-x 2 root root   4.0K Mar 16 12:38 rc0.d
drwxr-xr-x 2 root root   4.0K Mar 16 12:38 rc1.d
drwxr-xr-x 2 root root   4.0K Mar 16 12:38 rc2.d
drwxr-xr-x 2 root root   4.0K Mar 16 12:38 rc3.d
drwxr-xr-x 2 root root   4.0K Mar 16 12:38 rc4.d
drwxr-xr-x 2 root root   4.0K Mar 16 12:38 rc5.d
drwxr-xr-x 2 root root   4.0K Mar 16 12:38 rc6.d
-rwxr-xr-x 1 root root    262 Mar 10 17:39 rc.local
drwxr-xr-x 2 root root   4.0K Oct 13  2018 rcS.d
-rw-r--r-- 1 root root   4.9K Mar 18  2017 wgetrc

Detail of these directories:

root@tx:~# ls -lh /etc/profile.d
total 8.0K
-rw-r--r-- 1 root root 663 Apr 12  2016 bash_completion.sh
-rw-r--r-- 1 root root 161 Nov 20  2019 nix.sh
-rw-r--r-- 1 root root   0 Nov 20  2019 nix.sh.backup-before-nix

root@tx:~# ls -lh /etc/rcS.d/
total 0
lrwxrwxrwx 1 root root 20 Oct 13  2018 S01hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01kmod -> ../init.d/kmod
lrwxrwxrwx 1 root root 20 Jul 15  2017 S01networking -> ../init.d/networking
lrwxrwxrwx 1 root root 16 Jul 15  2017 S01procps -> ../init.d/procps
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01udev -> ../init.d/udev

root@tx:~# ls -lh /etc/rc0.d/
total 0
lrwxrwxrwx 1 root root 16 Jul 26  2018 K01docker -> ../init.d/docker
lrwxrwxrwx 1 root root 17 Mar 16 12:38 K01haproxy -> ../init.d/haproxy
lrwxrwxrwx 1 root root 20 Oct 13  2018 K01hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 20 Jul 15  2017 K01networking -> ../init.d/networking
lrwxrwxrwx 1 root root 15 Jul 27  2018 K01nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 20 Jan 23  2019 K01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 17 Jul 15  2017 K01rsyslog -> ../init.d/rsyslog

root@tx:~# ls -lh /etc/rc1.d/
total 0
lrwxrwxrwx 1 root root 16 Jul 26  2018 K01docker -> ../init.d/docker
lrwxrwxrwx 1 root root 17 Mar 16 12:38 K01haproxy -> ../init.d/haproxy
lrwxrwxrwx 1 root root 15 Jul 27  2018 K01nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 20 Jan 23  2019 K01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 17 Jul 15  2017 K01rsyslog -> ../init.d/rsyslog

root@tx:~# ls -lh /etc/rc2.d/
total 0
lrwxrwxrwx 1 root root 15 Jul 15  2017 S01acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01cron -> ../init.d/cron
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01dbus -> ../init.d/dbus
lrwxrwxrwx 1 root root 16 Jul 26  2018 S01docker -> ../init.d/docker
lrwxrwxrwx 1 root root 17 Mar 16 12:38 S01haproxy -> ../init.d/haproxy
lrwxrwxrwx 1 root root 21 Jul 17  2017 S01kdump-tools -> ../init.d/kdump-tools
lrwxrwxrwx 1 root root 15 Jul 17  2017 S01kexec -> ../init.d/kexec
lrwxrwxrwx 1 root root 20 Jul 17  2017 S01kexec-load -> ../init.d/kexec-load
lrwxrwxrwx 1 root root 15 Jul 27  2018 S01nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 20 Jan 23  2019 S01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 15 Jul 26  2018 S01rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root 17 Jul 15  2017 S01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 13 Jul 15  2017 S01ssh -> ../init.d/ssh
lrwxrwxrwx 1 root root 17 Jan 23  2019 S01sysstat -> ../init.d/sysstat

root@tx:~# ls -lh /etc/rc3.d/
total 0
lrwxrwxrwx 1 root root 15 Jul 15  2017 S01acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01cron -> ../init.d/cron
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01dbus -> ../init.d/dbus
lrwxrwxrwx 1 root root 16 Jul 26  2018 S01docker -> ../init.d/docker
lrwxrwxrwx 1 root root 17 Mar 16 12:38 S01haproxy -> ../init.d/haproxy
lrwxrwxrwx 1 root root 21 Jul 17  2017 S01kdump-tools -> ../init.d/kdump-tools
lrwxrwxrwx 1 root root 15 Jul 17  2017 S01kexec -> ../init.d/kexec
lrwxrwxrwx 1 root root 20 Jul 17  2017 S01kexec-load -> ../init.d/kexec-load
lrwxrwxrwx 1 root root 15 Jul 27  2018 S01nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 20 Jan 23  2019 S01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 15 Jul 26  2018 S01rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root 17 Jul 15  2017 S01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 13 Jul 15  2017 S01ssh -> ../init.d/ssh
lrwxrwxrwx 1 root root 17 Jan 23  2019 S01sysstat -> ../init.d/sysstat

root@tx:~# ls -lh /etc/rc4.d/
total 0
lrwxrwxrwx 1 root root 15 Jul 15  2017 S01acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01cron -> ../init.d/cron
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01dbus -> ../init.d/dbus
lrwxrwxrwx 1 root root 16 Jul 26  2018 S01docker -> ../init.d/docker
lrwxrwxrwx 1 root root 17 Mar 16 12:38 S01haproxy -> ../init.d/haproxy
lrwxrwxrwx 1 root root 21 Jul 17  2017 S01kdump-tools -> ../init.d/kdump-tools
lrwxrwxrwx 1 root root 15 Jul 17  2017 S01kexec -> ../init.d/kexec
lrwxrwxrwx 1 root root 20 Jul 17  2017 S01kexec-load -> ../init.d/kexec-load
lrwxrwxrwx 1 root root 15 Jul 27  2018 S01nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 20 Jan 23  2019 S01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 15 Jul 26  2018 S01rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root 17 Jul 15  2017 S01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 13 Jul 15  2017 S01ssh -> ../init.d/ssh
lrwxrwxrwx 1 root root 17 Jan 23  2019 S01sysstat -> ../init.d/sysstat

root@tx:~# ls -lh /etc/rc5.d/
total 0
lrwxrwxrwx 1 root root 15 Jul 15  2017 S01acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01cron -> ../init.d/cron
lrwxrwxrwx 1 root root 14 Jul 15  2017 S01dbus -> ../init.d/dbus
lrwxrwxrwx 1 root root 16 Jul 26  2018 S01docker -> ../init.d/docker
lrwxrwxrwx 1 root root 17 Mar 16 12:38 S01haproxy -> ../init.d/haproxy
lrwxrwxrwx 1 root root 21 Jul 17  2017 S01kdump-tools -> ../init.d/kdump-tools
lrwxrwxrwx 1 root root 15 Jul 17  2017 S01kexec -> ../init.d/kexec
lrwxrwxrwx 1 root root 20 Jul 17  2017 S01kexec-load -> ../init.d/kexec-load
lrwxrwxrwx 1 root root 15 Jul 27  2018 S01nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 20 Jan 23  2019 S01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 15 Jul 26  2018 S01rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root 17 Jul 15  2017 S01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 13 Jul 15  2017 S01ssh -> ../init.d/ssh
lrwxrwxrwx 1 root root 17 Jan 23  2019 S01sysstat -> ../init.d/sysstat

root@tx:~# ls -lh /etc/rc6.d/
total 0
lrwxrwxrwx 1 root root 16 Jul 26  2018 K01docker -> ../init.d/docker
lrwxrwxrwx 1 root root 17 Mar 16 12:38 K01haproxy -> ../init.d/haproxy
lrwxrwxrwx 1 root root 20 Oct 13  2018 K01hwclock.sh -> ../init.d/hwclock.sh
lrwxrwxrwx 1 root root 15 Jul 17  2017 K01kexec -> ../init.d/kexec
lrwxrwxrwx 1 root root 20 Jul 17  2017 K01kexec-load -> ../init.d/kexec-load
lrwxrwxrwx 1 root root 20 Jul 15  2017 K01networking -> ../init.d/networking
lrwxrwxrwx 1 root root 15 Jul 27  2018 K01nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 20 Jan 23  2019 K01postgresql -> ../init.d/postgresql
lrwxrwxrwx 1 root root 17 Jul 15  2017 K01rsyslog -> ../init.d/rsyslog

root@tx:~# ls -lh /etc/init.d
total 96K
-rwxr-xr-x 1 root root 2.3K Sep 20  2016 acpid
-rwxr-xr-x 1 root root 3.0K May  3  2015 cron
-rwxr-xr-x 1 root root 2.8K Apr  6  2017 dbus
-rwxr-xr-x 1 root root 3.8K Jul 19  2018 docker
-rwxr-xr-x 1 root root 3.4K Apr  4  2017 haproxy
-rwxr-xr-x 1 root root 3.8K Mar 22  2017 hwclock.sh
-rwxr-xr-x 1 root root 1.5K Jan  5  2017 kdump-tools
-rwxr-xr-x 1 root root 1.1K Jun 29  2016 kexec
-rwxr-xr-x 1 root root 3.3K Jun 29  2016 kexec-load
-rwxr-xr-x 1 root root 2.0K Dec 26  2016 kmod
-rwxr-xr-x 1 root root 4.5K Sep 16  2016 networking
-rwxr-xr-x 1 root root 4.5K Jul 12  2017 nginx
-rwxr-xr-x 1 root root 1.5K Jun  8  2018 postgresql
-rwxr-xr-x 1 root root 1.2K Nov 22  2016 procps
-rwxr-xr-x 1 root root 4.3K Dec 10  2017 rsync
-rwxr-xr-x 1 root root 2.9K Jan 19  2017 rsyslog
-rwxr-xr-x 1 root root 4.0K Mar 30  2017 ssh
-rwxr-xr-x 1 root root  731 Jun  5  2017 sudo
-rwxr-xr-x 1 root root 1.6K May 26  2017 sysstat
-rwxr-xr-x 1 root root 6.0K Jun  5  2017 udev

root@tx:~# ls -lh /etc/init
total 44K
-rw-r--r-- 1 root root 1.9K Jul 19  2018 docker.conf
-rw-r--r-- 1 root root  977 Jan  5  2017 kdump-tools.conf
-rw-r--r-- 1 root root 2.5K Jun  2  2015 networking.conf
-rw-r--r-- 1 root root  933 Jun  2  2015 network-interface.conf
-rw-r--r-- 1 root root  530 Jun  2  2015 network-interface-container.conf
-rw-r--r-- 1 root root 1.8K Jun  2  2015 network-interface-security.conf
-rw-r--r-- 1 root root  401 Jul 12  2017 nginx.conf
-rw-r--r-- 1 root root  637 Mar 30  2017 ssh.conf
-rw-r--r-- 1 root root  337 Jun  5  2017 udev.conf
-rw-r--r-- 1 root root  360 Jun  5  2017 udevmonitor.conf
-rw-r--r-- 1 root root  352 Jun  5  2017 udevtrigger.conf

root@tx:~# ls -lh /etc/systemd/
total 36K
-rw-r--r--  1 root root 1014 Jun  5  2017 journald.conf
-rw-r--r--  1 root root 1022 Jun  5  2017 logind.conf
drwxr-xr-x  2 root root 4.0K Jun  5  2017 network
-rw-r--r--  1 root root  642 Jun  5  2017 resolved.conf
drwxr-xr-x 10 root root 4.0K Nov 20  2019 system
-rw-r--r--  1 root root 1.6K Jul 20  2017 system.conf
-rw-r--r--  1 root root  607 Jun  5  2017 timesyncd.conf
drwxr-xr-x  2 root root 4.0K Jun  5  2017 user
-rw-r--r--  1 root root 1.2K Jun  5  2017 user.conf

/etc/rc.d/rc文件定义了服务启动的顺序是先K后S,而具体的每个运行级别的服务状态是放在/etc/rc.d/rc*.d(*=0~6)目录下,所有的文件均是指向/etc/init.d下相应文件的符号链接。

S表示的是启动时需要start的服务内容,K表示关机时需要关闭的服务内容。

如果我们需要自己增加启动的内容,可以在init.d目录中增加相关的shell脚本,然后在rc*.d目录中建立链接文件指向该shell脚本。这些shell脚本的启动或结束顺序是由S或K字母后面的数字决定,数字越小的脚本越先执行。

运行级别的含义:

1 Single user (super user)
2 multi user
3 multi user + network
4 not used / user definable
5 multi user, network + X11

/etc/init.d里的shell脚本能够响应start,stop,restart,reload命令来管理某个具体的应用。

root@tx:~# /etc/init.d/docker
Usage: service docker {start|stop|restart|status}
root@tx:~# /etc/init.d/nginx
Usage: nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}
root@tx:~# /etc/init.d/procps
Usage: /etc/init.d/procps {start|stop|restart|reload|force-reload|status}
root@tx:~# /etc/init.d/cron
[info] Usage: /etc/init.d/cron {start|stop|status|restart|reload|force-reload}.
root@tx:~# /etc/init.d/sudo
Usage: /etc/init.d/sudo {start|stop|restart|force-reload|status}
root@tx:~# /etc/init.d/networking
Usage: /etc/init.d/networking {start|stop|reload|restart|force-reload}
root@tx:~# /etc/init.d/sysstat
[FAIL] Usage: /etc/init.d/sysstat {start|stop|restart|reload|force-reload|status} ... failed!
root@tx:~# /etc/init.d/udev
Usage: /etc/init.d/udev {start|stop|restart|reload|force-reload|status}

/etc/init.d/就是旧时代liunx的用法,/etc/init/是现在Ubuntu的提倡并一步步转型的用法。为了平缓过渡,便让service命令可以同时寻找到两个文件夹。

如何添加脚本到开机启动项

添加到 /etc/rc.local

放置脚本到 /etc/profile.d/

通过 chkconfig / update-rc.d 命令

将启动文件cp到 /etc/init.d/或者/etc/rc.d/init.d/(前者是后者的软连接)下

vim 启动文件,文件前面务必添加如下三行代码,否侧会提示chkconfig不支持

#!/bin/sh 告诉系统使用的shell
# chkconfig: 35 20 80 分别代表运行级别分别是3和5(一共0到6),启动优先权,关闭优先权,此行代码必须
# description: http server(自己随便发挥),此行代码必须

chkconfig --add 脚本文件名,操作后就已经添加了

参考

comments powered by Disqus