说明
0——关机,
1——单用户,就是我们之前修改root账户密码的模式,
2——多用户模式,但比3模式少了一个nfs服务
3——多用户命令行模式,最常用
4——保留级别暂时没用,
5——图形模式,
6——重启
一、chkconfig-CentOS6以前
chkconfig就是CentOS6以前用来控制系统服务的工具,系统开机时启动的部分服务存储在/etc/init.d/目录下。我们可以把需要开机启动的服务放在这个目录下然后用chkconfig来管理。
查看命令
chkconfig --list #列出所有的系统服务。
操作命令
chkconfig --add httpd #增加httpd服务。
chkconfig --del httpd #删除httpd服务。
chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态。
chkconfig --list mysqld #列出mysqld服务设置情况。
chkconfig --level 35 mysqld on #设定mysqld在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表
示例
chkconfig --add nginx #添加nginx服务开机启动项
创建命令
(略)
二、systemd-CentOS6以后
查看命令
systemctl list-units --all --type=service #查看所有服务
systemctl list-units --type=service #查看所有已经启动的服务
操作命令
针对单一服务的
systemctl enable crond ##设置开机启动crond服务或工具
systemctl disable crond ##设置关闭开机启动crond服务或工具
systemctl status crond ##查看crond服务当前状态,如是否运行
systemctl stop crond ##停止crond服务是,但开机仍会运行
systemctl start crond ##开启crond服务
systemctl restart crond ##重启crond服务
systemctl is-enabled crond ##检查crond服务是否开机启动
示例:
systemctl enable nginx.service #添加nginx服务开机启动项
创建命令
vim /lib/systemd/system/nginx.service #在系统服务目录里创建nginx.service文件
内容:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
说明:
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
保存退出。
systemctl enable nginx.service #设置开机启动
三、chkconfig 和systemctl 对比
任 务 | 旧 指 令 | 新 指 令 |
---|---|---|
使某服务自动启动 | chkconfig –level 3 httpd on | systemctl enable httpd.service |
使某服务不自动启动 | chkconfig –level 3 httpd off | systemctl disable httpd.service |
检查服务状态 | service httpd status | systemctl status httpd.service |
显示所有已启动的服务 | chkconfig –list | systemctl list-units –type=service |
启动某服务 | service httpd start | systemctl start httpd.service |
停止某服务 | service httpd stop | systemctl stop httpd.service |
重启某服务 | service httpd restart | systemctl restart httpd.service |
四、对照表
1.Systemd 命令和 sysvinit 命令的对照表
2.Sysvinit 运行级别和 systemd 目标的对应表
参考文档
1.Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中:https://www.cnblogs.com/superlinux/p/bfd4812adffaccb36520279aaafcc160.html
2.Nginx+Center OS 7.2 开机启动设置:https://www.cnblogs.com/piscesLoveCc/p/5867900.html
3.Linux 设置程序开机自启动 (命令systemctl 和 chkconfig用法区别比较):https://blog.youkuaiyun.com/kenhins/article/details/74518978