Linux—开机启动过程详解

Linux系统有7个运行级别(runlevel)

1、级别有0-6共7个级别,其中2345是默认启动级别。

2、查看运行级别(centos6与centos7)

[root@localhost ~]# runlevel

3、切换运行级别(centos6与centos7)。

[root@localhost ~]# init 5       # 切换到运行级别5(临时切换的。若想修改默认的运行级别:/etc/inittab文件。)
[root@localhost ~]# init 0       # 关机命令
[root@localhost ~]# init 6       # 重启命令

4、查看运行级别(centos7)

[root@localhost ~]# systemctl get-default

5、切换运行级别(centos7)

[root@localhost ~]# systemctl set-default multi-user.target
[root@localhost ~]# systemctl isolate multi-user.target      # 在不重启的情况下,切换到运行级别mulit-user下。
[root@localhost ~]# systemctl isolate graphical.targ         # 在不重启的情况下,切换到图形界面下。等同于:init n

6、运行级别对比(centos6与centos7)

[root@VM_0_9_centos system]# ls /usr/lib/systemd/system -l | grep level | grep -v want
lrwxrwxrwx  1 root root   15 Nov  5  2019 runlevel0.target -> poweroff.target
lrwxrwxrwx  1 root root   13 Nov  5  2019 runlevel1.target -> rescue.target
lrwxrwxrwx  1 root root   17 Nov  5  2019 runlevel2.target -> multi-user.target
lrwxrwxrwx  1 root root   17 Nov  5  2019 runlevel3.target -> multi-user.target
lrwxrwxrwx  1 root root   17 Nov  5  2019 runlevel4.target -> multi-user.target
lrwxrwxrwx  1 root root   16 Nov  5  2019 runlevel5.target -> graphical.target
lrwxrwxrwx  1 root root   13 Nov  5  2019 runlevel6.target -> reboot.target

CentOS6与CentOS7开机启动项管理

1、将脚本移动到/etc/rc.d/init.d目录下

[root@localhost ~]# mv /www/wwwroot/MyServer.sh /etc/rc.d/init.d

2、增加脚本的可执行权限

[root@localhost ~]# chmod +x  /etc/rc.d/init.d/MyServer.sh

3、在脚本开头加入下面两句,才能使用 chkconfig 命令来运行安装服务。10是启动优先级,90是停止优先级,优先级范围是0-100,数字越大,优先级越低。

# chkconfig: 2345 10 90 
# description: myservice ....

4、将 MyServer.sh 添加到linux启动管理体系中(即linux系统服务列表中)。并设定 MyServer.sh 的开关(on/off)

[root@localhost ~]# chkconfig --add MyServer.sh
[root@localhost ~]# chkconfig MyServer.sh on

5、常见 chkconfig 命令

# 把 MyService服务 添加到系统服务列表中,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了
[root@localhost ~]# chkconfig --add MyService
# 把 MyService服务 从系统服务列表中删除
[root@localhost ~]# chkconfig --del MyService
# 查看全部服务的启动状态(在各运行级状态)
[root@localhost ~]# chkconfig --list
# 查看某个服务的启动状态(在各运行级状态)
[root@localhost ~]# chkconfig --list MyService

# 设定 mysqld 在各等级为on,“各等级”包括2、3、4、5。此时即为开机自启动
[root@localhost ~]# chkconfig mysqld on      
# 取消 mysqld 的开机自启动,只需要将 on 改为 off 即可: 
[root@localhost ~]# chkconfig mysqld off 

# 修改服务的默认启动等级:只有运行级别35启动,其他都关闭
[root@localhost ~]# chkconfig --level 24 mysqld off
# 修改服务的默认启动等级:只有运行级别35启动,其他都关闭
[root@localhost ~]# chkconfig --level 35 mysqld on

Ubuntu开机启动项管理

1、新建脚本文件 my_service,并设置权限

ubuntu@VM-0-16-ubuntu:~$ sudo chmod +x my_service

2、在脚本开头加入下面几句,不然服务管理会失败。

### BEGIN INIT INFO
# Provides:          my_service
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start my_service
# description:       start the my_service
### END INIT INFO

3、把脚本放置到启动目录下

ubuntu@VM-0-16-ubuntu:~$ sudo cp my_service /etc/init.d/    # 复制文件
ubuntu@VM-0-16-ubuntu:~$ sudo mv my_service /etc/init.d/    # 移动文件	

ubuntu@VM-0-16-ubuntu:~$ cd /etc/init.d	                    # 进入这个路径下

4、将脚本添加到开机启动脚本

ubuntu@VM-0-16-ubuntu:~$ sudo update-rc.d my_service defaults 90      # 90表明一个优先级,越高表示执行的越晚 (优先级范围:0~90)

5、服务管理

ubuntu@VM-0-16-ubuntu:~$ sudo service my_service stop
ubuntu@VM-0-16-ubuntu:~$ sudo service my_service start
ubuntu@VM-0-16-ubuntu:~$ sudo service my_service status
ubuntu@VM-0-16-ubuntu:~$ sudo service my_service restart
ubuntu@VM-0-16-ubuntu:~$ sudo service --status-all         # 查看全部服务列表

6、其它 update-rc.d 命令

sudo update-rc.d my_service defaults
sudo update-rc.d my_service defaults 90           # 启动和关闭顺序为90,级别默认
sudo update-rc.d my_service defaults 90 20        # 启动顺序90,关闭顺序20
sudo update-rc.d my_service start 90 2 3 4 5 . stop 20 0 1 6 .

sudo update-rc.d -f my_service remove                # 从所有的运行级别中删除指定启动项(即rc0.d-rc6.d目录下,都没有my_service文件了),但此时/etc/init.d/my_service仍然存在。
sudo update-rc.d my_service stop 80 0 1 2 3 4 5 6 .  # 禁用此服务开机自启。待测 ,好像不行。

CentOS7开机服务管理(可以设置启动项)

1、在centos7中,service脚本是在/usr/lib/systemd/system这个目录下。在ubuntu中,service脚本是在/lib/systemd/system这个目录下。脚本格式如下:

2、说明:启用服务就是在当前“runlevel”的配置文件目录/etc/systemd/system/multi-user.target.wants/里,建立/usr/lib/systemd/system里面对应服务配置文件的软链接;禁用服务就是删除此软链接,添加服务就是添加软连接。

原文地址:https://www.cnblogs.com/liuhaidon/p/11599586.html