Linux下chkconfig命令詳解即新增服務以及兩種方式啟動關閉系統服務

weixin_30639719發表於2020-04-05

The command chkconfig is no longer available in Ubuntu.The equivalent command to chkconfig is update-rc.d.This command nearly supports all the new versions of ubuntu.

The similar commands are

update-rc.d <service> defaults

update-rc.d <service> start 20 3 4 5

update-rc.d -f <service>  remove

sysv-rc-conf is an alternate option for Ubuntu.

sudo apt-get install sysv-rc-conf

sysv-rc-conf --list xxxx

In Ubuntu /etc/init.d has been replaced by /usr/lib/systemd. Scripts can still be started and stoped by 'service'. But the primary command is now 'systemctl'. The chkconfig command was left behind, and now you do this with systemctl.

So instead of:

chkconfig enable apache2

You should look for the service name, and then enable it

systemctl status apache2
systemctl enable apache2.service

Systemd has become more friendly about figuring out if you have a systemd script, or an /etc/init.d script, and doing the right thing.

//----------------------------------------------------------------------------------------------------------------------------------------------------------------

chkconfig命令主要用來更新(啟動或停止)和查詢系統服務的執行級資訊。謹記chkconfig不是立即自動禁止或啟用一個服務,它只是簡單的改變了符號連線。

一、chkconfig 的使用語法
1、chkconfig [--add][--del][--list][系統服務] 或 chkconfig [--level <levels等級代號>][系統服務][on/off/reset]

2、引數用法:
  --add  增加所指定的系統服務,讓chkconfig指令得以管理它,並同時在系統啟動的敘述檔案內增加相關資料。
  --del  刪除所指定的系統服務,不再由chkconfig指令管理,並同時在系統啟動的敘述檔案內刪除相關資料。
  --level<等級代號>  指定讀系統服務要在哪一個執行等級中開啟或關畢。
      等級0表示:表示關機
      等級1表示:單使用者模式
      等級2表示:無網路連線的多使用者命令列模式
      等級3表示:有網路連線的多使用者命令列模式
      等級4表示:系統保留
      等級5表示:帶圖形介面的多使用者模式
      等級6表示:重新啟動
 3、需要說明的是,level選項可以指定要檢視的執行級而不一定是當前執行級。對於每個執行級,只能有一個啟動指令碼或者停止指令碼。當切換執行級時,init不會重新啟動已經啟動的服務,也不會再次去停止已經停止的服務。

4、chkconfig --list [name]:顯示所有執行級系統服務的執行狀態資訊(on或off)。如果指定了name,那麼只顯示指定的服務在不同執行級的狀態。
5、chkconfig --add name:增加一項新的服務。chkconfig確保每個執行級有一項啟動(S)或者殺死(K)入口。如有缺少,則會從預設的init指令碼自動建立。
    chkconfig --del name:刪除服務,並把相關符號連線從/etc/rc[0-6].d刪除。
    chkconfig [--level levels] name:設定某一服務在指定的執行級是被啟動,停止還是重置。

6、執行級檔案:
每個被chkconfig管理的服務需要在對應的init.d下的指令碼加上兩行或者更多行的註釋。第一行告訴chkconfig預設啟動的執行級以及啟動和停止的優先順序。如果某服務預設不在任何執行級啟動,那麼使用 - 代替執行級。第二行對服務進行描述,可以用\ 跨行註釋。
例如,random.init包含三行:
# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
# higher quality random number generation.

使用範例:
chkconfig --list        #列出所有的系統服務
chkconfig --add httpd        #增加httpd服務
chkconfig --del httpd        #刪除httpd服務
chkconfig --level  2345  httpd  on        #設定httpd在執行級別為2、3、4、5的情況下都是on(開啟)的狀態
chkconfig --list        #列出系統所有的服務啟動情況
chkconfig --list mysqld        #列出mysqld服務設定情況
chkconfig --level 35 mysqld on        #設定mysqld在等級3和5為開機執行服務,--level 35表示操作只在等級3和5執行,on表示啟動,off表示關閉
chkconfig mysqld on        #設定mysqld在各等級為on,“各等級”包括2、3、4、5等級

7、如何增加一個服務:以mysqld 為例
(1)、服務指令碼必須存放在/etc/init.d/目錄下;
(2)、chkconfig --add mysqld  #新增服務,在chkconfig工具服務列表中增加此服務,此時服務會被在/etc/rc.d/rcN.d中賦予K/S入口;
(3)、chkconfig --level 35 mysqld on  #  修改服務的預設啟動等級。

二、服務的啟動/關閉
1、系統服務:一直在記憶體中,而且一直在執行,並提供服務的被稱為服務;

2、而服務也是一個執行的程式,則這個執行的程式則被稱為daemons;

3、這些服務的啟動指令碼一般放置在: /etc/init.d

4、在CentOS中服務啟動指令碼放置在:/etc/rc.d/init.d而/etc/init.d這個目錄為公認的目錄,在centos中/etc/init.d就是一個連結檔案

5、/etc/sysconfig  服務初始化環境變數配置都在這個檔案中。

6、/var/lib  各個服務產生的資料庫都在這個目錄下,最簡單的在這裡找到 mysql 使用 vim 開啟就可以看到,你建立的資料庫以及系統預設產生的資料庫名稱都在這裡面!

7、啟動/停止/重啟服務 :  /etc/init.d/ serverName  start/stop /restart/status

8、啟動/停止/重啟服務:  service  serverName  start/stop/restart  為什麼可以這樣寫?

9、service 是一個script  他可以分析你後面下達的引數,然後根據你的引數在到/etc/init.d 下去取得正確的服務來 stop  start  restart

轉載於:https://www.cnblogs.com/jukan/p/5681803.html

相關文章