Centos7 环境下开机 自启动服务(service) 设置的改变 (命令systemctl 和 chkconfig用法区别比较)
参考文章: 《Linux 设置程序开机自启动 (命令systemctl 和 chkconfig用法区别比较)》
http://blog.csdn.net/kenhins/article/details/74518978
最近看书发现 centos 中开机自启动的设置在不同版本中有所不同,发现如下:


其中,最大的一个不同是在 centos7中 chkconfig 命令会有如下提示:
注:该输出结果只显示 SysV 服务,并不包含 原生 systemd 服务。SysV 配置数据 可能被原生 systemd 配置覆盖。 要列出 systemd 服务,请执行 'systemctl list-unit-files'。 查看在具体 target 启用的服务请执行 'systemctl list-dependencies [target]'。
这也就是说 在 centos7 中 chkconfig 命令与以前版本中 不同。
下面给出网上的一些资料:
centos 7 怎样设置开机自动启动, 如 httpd和mysqld 服务
systemctl enable httpd.service
systemctl enable mysqld.service
如果要取消开机自动启动apache和mysql,则用下面的命令
 systemctl disable httpd.service
      
systemctl disable mysqld.service
在以前版本中的    centos   系统中 开机自动运行httpd服务好像不是上面的命令,而是
      
chkconfig –level 3 httpd on
| 任务 | 旧指令 | 新指令 | 
| 使某服务自动启动 | 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 | 
systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
在centos系统启动过程中,会根据 运行级别 执行 /etc/rcx.d/* (其中x为运行级别)。这里的文件实际上软链接到 /etc/init.d/ 下的脚本文件。


systemctl 和 chkconfig 命令都是自动设置自启动项目的命令:
 systemctl 命令:  
设置开机启动指令:systemctl enable tomcat  
取消设置:systemctl disable tomcat  
查看设置:systemctl list-unit-files 
chkconfig 命令: 使用chkconfig时需要注意,/etc/init.d/下有对应的脚本(这里就是tomcat),且该脚本头部包含如下注释以支持chkconfig, 注释表示在rc2|3|4|5.d下生成优先级96的启动脚本软链接,另外在rc0|6.d下生成优先级04的停止脚本软链接: #chkconfig: 2345 96 04 设置开机启动指令: chkconfig --add tomcat 取消设置: chkconfig --del tomcat 查看设置: chkconfig
正常我们开机是使用 level5 的, 于是进入 rc5.d 目录中查看:


个人的感觉是在 Centos7 中 chkconfig 命令属于被替换的那种命令, 毕竟 chkconfig命令使用起来还是要比 systemctl 要复杂一些的。
而且 systemctl 提示会覆盖 chkconfig 命令的设置, 具体是什么意思不是很理解,有可能是说在systemctl 中设置过了 在 chkconfig 中进行命令是没有效果的, 比如我在systemctl 中设置 tomcat 不自启动, 但是在 chkconfig 中设置自启动,但是实际上 chkconfig 的设置是无效的,也就是说 systemctl 命令是真正执行的。
网上有网友发现以下事情:
先用systemctl设置开机启动,但是因为它是从/usr/lib/systemd/system/下的软链接到/etc/systemd/system/multi-user.target.wants,
而奇怪的是/usr/lib/systemd/system/tomcat这个文件中的环境配置是错误的。 为了防止其他不必要的麻烦,决定使用chkconfig,用chkconfig设置开机启动后重启机子,
发现tomcat未启动。orz。后来尝试把文件名tomcat改成test,执行chkconfig --add test后重启机子,发现tomcat启动了。
posted on 2018-02-27 15:25 Angry_Panda 阅读(2217) 评论(0) 收藏 举报
 
                    
                     
                    
                 
                    
                 
                
            
         
         
 浙公网安备 33010602011771号
浙公网安备 33010602011771号