日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux服务与chkconfig

發布時間:2025/3/15 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux服务与chkconfig 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

chkconfig使用說明

chkconfig是管理系統服務(service)的命令行工具。所謂系統服務(service),就是隨系統啟動而啟動,隨系統關閉而關閉的程序。

chkconfig可以更新(啟動或停止)和查詢系統服務(service)運行級信息。更簡單一點,chkconfig是一個用于維護/etc/rc[0-6].d目錄的命令行工具。

chkconfig 提供5個功能:

1. 設置service啟動信息

# chkconfig name on/off/reset

on、off、reset用于改變service的啟動信息。?
on表示開啟,off表示關閉,reset表示重置。?
默認情況下,on和off開關只對運行級2,3,4,5有效,reset可以對所有運行級有效。?例如,

2. 設置service運行級別

# chkconfig --level levels

例如,

# chkconfig --level 2345 httpd on

指定運行級為2,3,4,5?
等級0表示:表示關機?
等級1表示:單用戶模式?
等級2表示:無網絡連接的多用戶命令行模式?
等級3表示:有網絡連接的多用戶命令行模式?
等級4表示:不可用?
等級5表示:帶圖形界面的多用戶模式?
等級6表示:重新啟動

3. 添加service

# chkconfig --add name

添加一個chkconfig管理的service,并在/etc/rc[0-6].d 目錄下添加相應的符號鏈接(symbolic links)。

4. 移除service

# chkconfig --del name

從chkconfig 管理名單中刪除該service,并且刪除 /etc/rc[0-6].d 目錄下所有與之關聯的符號鏈接(symbolic links)。

5. 列出service的啟動信息

# chkconfig --list [name]

如果不指定name,會列出所有services的信息。

每個service每個運行級別都會有一個啟動和停止腳本;當切換運行級別時,init不會重啟已經啟動的service,也不會重新停止已經停止的service。

下面舉例說明

(1).列出所有服務的啟動情況

$ chkconfig --list auditd ? ? ? ? ?0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off redis ? ? ? ? ? 0:off ? 1:off ? 2:off ? 3:off ? 4:off ? 5:off ? 6:off restorecond ? ? 0:off ? 1:off ? 2:off ? 3:off ? 4:off ? 5:off ? 6:off rpcbind ? ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off rpcgssd ? ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:on ? ?5:on ? ?6:off rpcsvcgssd ? ? ?0:off ? 1:off ? 2:off ? 3:off ? 4:off ? 5:off ? 6:off rsyslog ? ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off saslauthd ? ? ? 0:off ? 1:off ? 2:off ? 3:off ? 4:off ? 5:off ? 6:off smb ? ? ? ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:off ? 5:on ? ?6:off 。。。 xinetd based services:rsync: ? ? ? ? ?offswat: ? ? ? ? ? off

(2)增加mysqld服務

$ chkconfig --add mysqld

(3)刪除mysqld服務

$ chkconfig --del mysqld

(4)設置mysqld運行級別為2,3,4,5

$ chkconfig --level 2345 httpd on

(5)列出mysqld 服務啟動信息情況

$ chkconfig --list mysqld mysqld ? ? ? ? ?0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off

(6)設置啟動信息

$ chkconfig mysqld on

默認的運行級別為2,3,4,5?
實際上,與4中命令作用是一樣的。
?

====================================================

添加linux服務

所謂系統服務(service),就是隨系統啟動而啟動,隨系統關閉而關閉的程序。更通俗的講,就是進程的開機啟動。?
本文主要介紹如何添加一個service。

1. 開發一個程序
首先開發一個軟件,使其成為service。代碼如下:

//capsule.c #include <unistd.h> #include <time.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <signal.h>int init_daemon();int main() { ? ? ??init_daemon();while(1){ ? ? ??sleep(2);}return 0; }int init_daemon() {int i;pid_t ? pid;if ( (pid = fork()) < 0){return (-1);}else if (pid){_exit(0); ? ? ? ? ? ? ? ? ? ? ? /* parent terminates */}/* child 1 continues... */if (setsid() < 0) ? ? ? ? ? ? ? ? ? ? ? /* become session leader */{return (-1);}signal(SIGHUP, SIG_IGN);if ( (pid = fork()) < 0){return (-1);}else if (pid){_exit(0); ? ? ? ? ? ? ? ? ? ? ? /* child 1 terminates */}chdir("/"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? /* change working directory *//* close off file descriptors */for (i = 0; i < 255; i++){close(i);}/* redirect stdin, stdout, and stderr to /dev/null */open("/dev/null", O_RDONLY);open("/dev/null", O_RDWR);open("/dev/null", O_RDWR);return (0); } ??

init_daemon()完成進程的精靈化過程,包括脫離終端轉入后臺等。

編譯

# gcc capsule.c –o capsule

將可執行文件拷貝到/usr/local/sbin目錄下

# cp capsule /usr/local/sbin/

2.編寫service腳本
service一般通過chkconfig工具進行管理。chkconfig管理的每個service需要在其init.d腳本中添加兩行或更多注釋行。?
第一行告訴chkconfig 該service運行的默認級別,以及啟動和停止的優先權。如果該service不在任何運行級啟動,可以設置為”-“。?
第二行包含描述信息。

針對剛才的可執行文件,編寫service腳本capsuled如下:

#!/bin/bash # # capsuled ? ?A test service program # # chkconfig: - 92 12 # description: A test service prog # # @name: capsuled # @author: lanyang # @created: 2017.01.23 # # Source function library. . /etc/init.d/functionsPROG=capsuled RETVAL=0 FULL_PATH=/usr/local/sbin/capsulestart() {echo -n $"Starting $PROG ..."daemon $FULL_PATHRETVAL=$?echo }stop() {echo -n $"Stopping $PROG ..."killproc $FULL_PATHRETVAL=$?echo }case "$1" instart)start;;stop)stop;;restart)stopsleep 5startRETVAL=$?;;status)status $FULL_PATHRETVAL=$?;;*)echo $“Usage: $0 {start|stop|restart|status}”exit 1esacexit $RETVAL

3.在/etc/init.d/目錄下添加service腳本

# cp capsuled /etc/init.d # chmod a+x capsuled

4.添加service,使chkconfig命令可以管理該service

# chkconfig --add capsuled

5.設置service啟動運行級別

# chkconfig --level 2345 capsuled on

查看啟動信息

# chkconfig --list capsuled capsuled ? ? ? ?0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off

6.啟動service

?

# service capsuled start Starting capsuled ... ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?] # ps -ef | grep capsule root ? ? ?17729 ? ? ?1 ?0 18:31 ? ? ? ? ?00:00:00 /usr/local/sbin/capsule# service capsuled status capsule (pid 17729) is running...# service capsuled stop Stopping capsuled ... ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]

7.小結

# service ?capsuled start

# /etc/init.d/capsuled start

作用是一樣的。實際上,前者是通過調用后者實現的。

管理service使用的chkconfig是一個用于維護/etc/rc[0-6].d目錄的命令行工具。其中,[0-6]指的是系統的7個運行級別。?
/etc/rc[0-6].d目錄,內容全部是鏈接(symbolic links),一般鏈接到/etc/init.d/目錄下的某個service腳本文件。?
例如,

$ ll /etc/rc5.d/S85httpd? lrwxrwxrwx. 1 root root 15 Jul 30 ?2015 /etc/rc5.d/S85httpd -> ../init.d/httpd

其中,85是啟動優先級;

$ ll /etc/rc6.d/K15httpd? lrwxrwxrwx. 1 root root 15 Jul 30 ?2015 /etc/rc6.d/K15httpd -> ../init.d/httpd

其中,15是停止優先級。?
類似的,

# ll /etc/rc4.d/ | grep capsuled lrwxrwxrwx. 1 root root 18 Jan 23 17:53 S92capsuled -> ../init.d/capsuled# ll /etc/rc5.d/ | grep capsuled lrwxrwxrwx. 1 root root 18 Jan 23 17:53 S92capsuled -> ../init.d/capsuled

92是啟動優先級

# ll /etc/rc6.d/ | grep capsuled lrwxrwxrwx. 1 root root 18 Jan 23 17:52 K12capsuled -> ../init.d/capsuled

12是停止優先級

新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的Linux服务与chkconfig的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。