10.24 chkconfig工具

Linux系統服務管理-chkconfig

  • chkconfig --list
  • chkconfig --level 3 network off
  • chkconfig --level 345 network off
  • chkconfig --del network
  • chkconfig --add network

chkconfig工具

  • crond、iptables、firewalld、nginx、httpd、mysql等等,都屬於服務。
  • chkconfig工具,在centos6和以前的版本中,控制服務的啓動;但在centos7中不多使用了,但爲了兼容以前的版本,依然能夠使用,但在將來的趨勢中, 有可能就會被遺棄了,如今就是過分的做用。
  • chkconfig --list //列出全部的系統服務
    • 表示chkconfig工具在centos6或以前的版本中,使用的服務的管理的機制叫 SysV,而centos7的版本中,使用的是 systemd 服務
[root@hf-01 ~]# chkconfig --list    //列出全部的系統服務

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:開	3:開	4:開	5:開	6:關
[root@hf-01 ~]#

chkconfig命令

  • 服務的腳本存放在 /etc/init.d/ 下面
    • 啓動腳本存放該目錄下
[root@hf-01 ~]# ls /etc/init.d/
functions  netconsole  network  README
[root@hf-01 ~]#
  • chkconfig --list //列出全部的服務
  • chkconfig network off //將network服務關閉
[root@hf-01 ~]# chkconfig network off
[root@hf-01 ~]# chkconfig --list    //會看到2,3,4級別關閉了

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:關	3:關	4:關	5:關	6:關
[root@hf-01 ~]# chkconfig network on
[root@hf-01 ~]# chkconfig --list    //會看到2,3,4級別又開啓了

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:開	3:開	4:開	5:開	6:關
[root@hf-01 ~]#
  • 在系統中有七個級別等級列表:mysql

    • 等級0表示:表示關機
    • 等級1表示:單用戶模式
    • 等級2表示:多用戶模式,少nfs服務
    • 等級3表示:多用戶模式,不帶圖形
    • 等級4表示:是一種保留的級別
    • 等級5表示:帶圖形界面的多用戶模式
    • 等級6表示:從新啓動
  • 在centos6中的 /etc/inittab 中定義開機的級別nginx

  • 在centos7中,已經沒有用了,不須要定義開機的級別了sql

chkconfig命令,指定某一級別開啓/關閉

  • chkconfig --level 3 network off //指定network中的3級別關閉
[root@hf-01 ~]# chkconfig --level 3 network off        //指定network中的3級別關閉
[root@hf-01 ~]# chkconfig --list        //列出全部服務

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:開	3:關	4:開	5:開	6:關
[root@hf-01 ~]#
  • chkconfig --level 345 network on //指定network中的3,4,5級別開啓
[root@hf-01 ~]# chkconfig --level 345 network on        //指定network中的3,4,5級別開啓
[root@hf-01 ~]# chkconfig --list

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:開	3:開	4:開	5:開	6:關
[root@hf-01 ~]#
  • 0和1和6級別不能設置成開
    • 0級別在關機狀態是不可能開啓的
    • 1級別是單用戶模式,服務是不可能開啓的
    • 6級別在重啓的時候,是不可能開啓的——>重啓至關於先關閉在啓動(重啓的那一刻是先關閉纔對)。

將一個腳本加入到服務列表中

  1. 首先將啓動腳本放入到 /etc/init.d 這個目錄下——>只有在這個目錄下,才能夠添加到服務列表中去
  2. 文件名稱無所謂,但內容有格式要求
    • 首先是是一個shell腳本
    • 而後chkconfig指定運行級別啓動順序,第10位啓動,第90位關閉
    • 下面代碼爲它的固定格式,必需要有的!!!
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
#              start at boot time.
  • 例子:
[root@hf-01 ~]# cd /etc/init.d
[root@hf-01 init.d]# ls
functions  netconsole  network  README
[root@hf-01 init.d]# cp network 123
[root@hf-01 init.d]# ls -l
總用量 40
-rwxr-xr-x  1 root root  7293 12月  5 05:27 123
-rw-r--r--. 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x. 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x. 1 root root  7293 5月   3 2017 network
-rw-r--r--. 1 root root  1160 10月 20 11:07 README
[root@hf-01 init.d]# chkconfig --list

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:開	3:開	4:開	5:開	6:關
[root@hf-01 init.d]# chkconfig --add 123    //將123加入到服務列表中
[root@hf-01 init.d]# chkconfig --list

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

123            	0:關	1:關	2:開	3:開	4:開	5:開	6:關
netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:開	3:開	4:開	5:開	6:關
[root@hf-01 init.d]# chkconfig --del 123    //刪除服務列表中的腳本
[root@hf-01 init.d]# chkconfig --list

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      若是您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

netconsole     	0:關	1:關	2:關	3:關	4:關	5:關	6:關
network        	0:關	1:關	2:開	3:開	4:開	5:開	6:關
[root@hf-01 init.d]#
  • chkconfig --del network //刪除服務列表中的腳本
  • chkconfig --add network //增長服務列表中的腳本
相關文章
相關標籤/搜索