chkconfig是檢查,設定系統的各種服務。
基本介紹
- 外文名:chkconfig
- 包含:檢查,設定系統的各種服務
- 語 法:chkconfig [--add][--del]
- 包括:各類常駐服務
- 使用環境:linux
功能說明,命令介紹,
功能說明
檢查,設定系統的各種服務。
語 法:chkconfig [--add][--del][--list][系統服務] 或 chkconfig [--level <等級代號>][系統服務][on/off/reset]
補充說明:這是Red Hat公司遵循GPL規則所開發的程式,它可查詢作業系統在每一個執行等級中會執行哪些系統服務,其中包括各類常駐服務。
chkconfig命令主要用來更新(啟動或停止)和查詢系統服務的運行級信息。謹記chkconfig不是立即自動禁止或激活一個服務,它只是簡單的改變了符號連線。
語法:
chkconfig --list [name]
chkconfig --add name
chkconfig --del name
chkconfig [--level levels] name <on|off|reset>
chkconfig [--level levels] name
chkconfig 沒有參數運行時,顯示用法。如果在服務名後面指定了on,off或者reset,那么chkconfig 會改變指定服務的啟動信息。on和off分別指服務被啟動和停止,reset指重置服務的啟動信息,無論有問題的初始化腳本指定了什麼。on和off開關,系統默認只對運行級2,3,4,5有效,但是reset可以對所有運行級有效。
--level選項可以指定要查看的運行級而不一定是當前運行級。
--level<等級代號> 指定讀系統服務要在哪一個執行等級中開啟或關閉。
等級0表示:表示關機
等級1表示:單用戶模式
等級2表示:無網路連線的多用戶命令行模式
等級3表示:有網路連線的多用戶命令行模式
等級4表示:不可用
等級5表示:帶圖形界面的多用戶模式
等級6表示:重新啟動
需要說明的是,level選項可以指定要查看的運行級而不一定是當前運行級。對於每個運行級,只能有一個啟動腳本或者停止腳本。當切換運行級時,init不會重新啟動已經啟動的服務,也不會再次去停止已經停止的服務。
chkconfig --list :顯示所有運行級系統服務的運行狀態信息(on或off)。如果指定了name,那么只顯示指定的服務在不同運行級的狀態。
chkconfig --add name:增加一項新的服務。chkconfig確保每個運行級有一項啟動(S)或者殺死(K)入口。如有缺少,則會從預設的init腳本自動建立。
chkconfig --del name:刪除所指定的系統服務,不再由chkconfig指令管理,並同時在系統啟動的敘述檔案內刪除相關數據。
命令介紹
Common mode
Format: service <service>
Print the specified service <service> the command line help.
Format: service <service> start
Starts the specified system services <service>
Format: service <service> stop
Stop the specified system service <service>
Format: service <service> restart
Restart the specified system service <service>, the first stop (stop), and then start (start).
Format: chkconfig - list
View list of system services, and operation of each service level.
Format: chkconfig <service> on
<service> Designated service set to start automatically when booting.
Format: chkconfig <service> off
Set the specified service does not start automatically at boot time <service>.
Format: ntsysv
Full-screen text interface to set whether to automatically start services at boot time.
Use examples
Example a network restart
When you modify a host name, ip address and other information, the network often need to restart to take effect.
[Root @ node34 root] # service network
Usage: / etc / init.d / network {start | stop | restart | reload | status}
[Root @ node34 root] # service network status
Configure devices:
lo eth0
The current active device:
lo eth0
[Root @ node34 root] # service network restart
Shutting down the interface eth0: [OK]
Loopback interface: [OK]
Setting network parameters: [OK]
Pop-up loopback interface: [OK]
Pop-up interface eth0: [OK]
[Root @ node34 root] #
Example two restart MySQL
[Root @ node34 root] # service mysql
mysql: unrecognized service
[Root @ node34 root] # service mysqld
Usage: / etc / init.d / mysqld {start | stop | status | condrestart | restart}
[Root @ node34 root] # service mysqld status
mysqld (pid 1638) is running ...
[Root @ node34 root] # service mysqld restart
Stop MySQL: [OK]
Startup MySQL: [determined]
[Root @ node34 root] #
Sample source code showing three service script [Root @ web ~] # cat / sbin / service
#! / Bin / sh
. / Etc / init.d / functions
VERSION = "` basename $ 0 `ver. 0.91"
USAGE = "Usage:` basename $ 0 `<option> | - status-all | \
[Service_name [command | - full-restart]] "
SERVICE =
SERVICEDIR = "/ etc / init.d"
OPTIONS =
if [$ #-eq 0]; then
echo "$ {USAGE}"> & 2
exit 1
fi
cd /
while [$ #-gt 0]; do
case "$ {1}" in
- Help |-h | - h *)
echo "$ {USAGE}"> & 2
exit 0
;;
- Version |-V)
echo "$ {VERSION}"> & 2
exit 0
;;
*)
if [-z "$ {SERVICE}"-a $ #-eq 1-a "$ {1}" = "- status-all"]; then
cd $ {SERVICEDIR}
for SERVICE in *; do
case "$ {SERVICE}" in
functions | halt | killall | single | linuxconf | kudzu)
;;
*)
if! is_ignored_file "$ {SERVICE}" \
& & [-X "$ {SERVICEDIR} / $ {SERVICE}"]; then
env-i LANG = "$ LANG" PATH = "$ PATH" TERM = "$ TERM" "$ {SERVICEDIR} / $ {SERVICE}" status
fi
;;
esac
done
exit 0
elif [$ #-eq 2-a "$ {2}" = "- full-restart"]; then
SERVICE = "$ {1}"
if [-x "$ {SERVICEDIR} / $ {SERVICE}"]; then
env-i LANG = "$ LANG" PATH = "$ PATH" TERM = "$ TERM" "$ {SERVICEDIR} / $ {SERVICE}" stop
env-i LANG = "$ LANG" PATH = "$ PATH" TERM = "$ TERM" "$ {SERVICEDIR} / $ {SERVICE}" start
exit $?
fi
elif [-z "$ {SERVICE}"]; then
SERVICE = "$ {1}"
else
OPTIONS = "$ {OPTIONS} $ {1}"
fi
shift
;;
esac
done
if [-x "$ {SERVICEDIR} / $ {SERVICE}"]; then
env-i LANG = "$ LANG" PATH = "$ PATH" TERM = "$ TERM" "$ {SERVICEDIR} / $ {SERVICE}" $ {OPTIONS}
else
echo $ "$ {SERVICE}: unrecognized service"> & 2
exit 1
fi
[Root @ web ~] #
Example 4 crond service source
[Root @ web init.d] # cat / etc / init.d / crond
#! / Bin / bash
#
# Crond Start / Stop the cron clock daemon.
#
# Chkconfig: 2345 90 60
# Description: cron is a standard UNIX program that runs user-specified \
# Programs at periodic scheduled times. Vixie cron adds a \
# Number of features to the basic UNIX cron, including better \
# Security and more powerful configuration options.
# Processname: crond
# Config: / etc / crontab
# Pidfile: / var / run / crond.pid
# Source function library.
. / Etc / init.d / functions
. / Etc / sysconfig / crond
t = $ {CRON_VALIDATE_MAILRCPTS:-UNSET}
["$ T"! = "UNSET"] & & export CRON_VALIDATE_MAILRCPTS = "$ t"
# See how we were called.
prog = "crond"
start () {
echo-n $ "Starting $ prog:"
if [-e / var / lock / subsys / crond]; then
if [-e / var / run / crond.pid] & & [-e / proc / `cat / var / run / crond.pid`]; then
echo-n $ "cannot start crond: crond is already running.";
failure $ "cannot start crond: crond already running.";
echo
return 1
fi
fi
daemon crond $ CRONDARGS
RETVAL = $?
echo
[$ RETVAL-eq 0] & & touch / var / lock / subsys / crond;
return $ RETVAL
}
stop () {
echo-n $ "Stopping $ prog:"
if [!-e / var / lock / subsys / crond]; then
echo-n $ "cannot stop crond: crond is not running."
failure $ "cannot stop crond: crond is not running."
echo
return 1;
fi
killproc crond
RETVAL = $?
echo
[$ RETVAL-eq 0] & & rm-f / var / lock / subsys / crond;
return $ RETVAL
}
rhstatus () {
status crond
}
restart () {
stop
start
}
reload () {
echo-n $ "Reloading cron daemon configuration:"
killproc crond-HUP
RETVAL = $?
echo
return $ RETVAL
}
case "$ 1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
rhstatus
;;
condrestart)
[-F / var / lock / subsys / crond] & & restart | |:
;;
*)
echo $ "Usage: $ 0 {start | stop | status | reload | restart | condrestart}"
exit 1
esac
[Root @ web init.d] #