參考文章: 《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啟動了。