Linux 设置程序开机自启动 (命令systemctl 和 chkconfig用法区别比较)

来源:互联网 发布:unity3d数学基础 编辑:程序博客网 时间:2024/05/22 17:16

之前在Linux centos 7 上安装了apache 和mysql,当时并没有设置开机自动启动。

最近又重新练习网页,每次开机总是要手动启动httpd和mysqld,不方便,就想设置成开机自动启动apache和mysql。

Linux centos 7 怎样设置开机自动启动httpd和mysqld服务呢?

我在网上找到了下面两条命令:

systemctl enable httpd.service

systemctl enable mysqld.service

如果要取消开机自动启动apache和mysql,则用下面的命令

systemctl disable httpd.service
systemctl disable mysqld.service

可是,我想起了以前我看过开机自动运行httpd服务好像不是上面的命令,而是
chkconfig –level 3 httpd on

那么chkconfig 和systemctl 这两个命令又有什么区别与联系呢?

我在网上找到了下面这个关于systemctl 和chkconfig 指令用法比较的表格

任务旧指令新指令使某服务自动启动chkconfig –level 3 httpd onsystemctl enable httpd.service使某服务不自动启动chkconfig –level 3 httpd offsystemctl disable httpd.service检查服务状态service httpd statussystemctl status httpd.service显示所有已启动的服务chkconfig –listsystemctl list-units –type=service启动某服务service httpd startsystemctl start httpd.service停止某服务service httpd stopsystemctl stop httpd.service重启某服务service httpd restartsystemctl restart httpd.service


















systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig 这两个命令组合到一起

centos7开机启动程序

2、有两种方法可以解决:

a.在系统启动过程中,会根据运行级别执行/etc/rcx.d/*(其中x为运行级别)。这里的文件实际上软链接到/etc/init.d/下的脚本文件,

那么,怎么创建这些软链接呢?可以使用systemctl或chkconfig指令(它们的关系参考RHEL 7 中 systemctl 的用法(替代service 和 chkconfig))。

[html] view plain copy
  1. systemctl 指令实例:  
  2. 设置开机启动指令:systemctl enable tomcat  
  3. 取消设置:systemctl disable tomcat  
  4. 查看设置:systemctl list-unit-files  


[html] view plain copy
  1. chkconfig 指令实例:  
  2. 使用chkconfig时需要注意,/etc/init.d/下有对应的脚本(这里就是tomcat),且该脚本头部包含如下注释以支持chkconfig,  
  3. 注释表示在rc2|3|4|5.d下生成优先级96的启动脚本软链接,另外在rc0|6.d下生成优先级04的停止脚本软链接:  
  4. #chkconfig: 2345 96 04  
  5.   
  6. 设置开机启动指令: chkconfig --add tomcat  
  7. 取消设置: chkconfig --del tomcat  
  8. 查看设置: chkconfig  


这里,我先用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启动了。Orz。再后来执行chkconfig指令时发现这么一段话。

[html] view plain copy
  1. 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。   
  2.       如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。  
  3.       欲查看对特定 target 启用的服务请执行  
  4.       'systemctl list-dependencies [target]'。  
恩,看了下systemctl list-unit-files的执行结果,"tomcat.servicedisabled"。估计chkconfig的设置被systemctl的覆盖了。

所以现在的情况是,用systemctl设置的因为配置错误不能成功启动tomcat,而不用systemctl的设置的话,chkconfig的设置又会被systemctl覆盖。MDZZ。

如果你照着我的配置走到这里,想骂娘?别急,除了把文件名tomcat改掉之外,我们可以剑走偏峰。请看方法b。


b.借用/etc/rc.d/rc.local实现开机启动:

在Linux系统启动过程中,在执行完对应运行级别的脚本文件(/etc/rcx.d/*)后,还会执行/etc/rc.local(前提是该文件权限是可执行的)。

所以我直接在/etc/rc.local最后加上

[html] view plain copy
  1. /usr/local/tomcat/bin/startup.sh  
同时,为了在系统重启/关闭时停止tomcat,执行下面两条指令:

[html] view plain copy
  1. ln -s /etc/init.d/tomcat /etc/rc0.d/K04tomcat  
  2. ln -s /etc/init.d/tomcat /etc/rc6.d/K04tomcat  



阅读全文
0 0
原创粉丝点击