首頁(yè) 資訊 linux service命令解析(重要)

linux service命令解析(重要)

來(lái)源:泰然健康網(wǎng) 時(shí)間:2024年12月29日 16:39

我們平時(shí)都會(huì)用service xxx start來(lái)啟動(dòng)某個(gè)進(jìn)程,那么它背后究竟執(zhí)行了什么?

其實(shí)service的絕對(duì)路徑為/sbin/service ,打開(kāi)這個(gè)文件cat /sbin/service,我們會(huì)發(fā)現(xiàn)其實(shí)它就是一個(gè)很普通的shell腳本

[root@zejin240 ~]# 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 …… 一些參數(shù)條件判斷 if [ -f "${SERVICEDIR}/${SERVICE}" ]; then env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" ${OPTIONS} else echo $"${SERVICE}: unrecognized service" >&2 exit 1 fi

其實(shí)這個(gè)腳本service主要作了如下兩點(diǎn):

1.初始化執(zhí)行環(huán)境變量PATH和TERM

PATH=/sbin:/usr/sbin:/bin:/usr/bin

TERM,為顯示外設(shè)的值,一般為xterm

2.調(diào)用/etc/init.d/文件夾下的相應(yīng)腳本,腳本的參數(shù)為service命令第二個(gè)及之后的參數(shù)

以service mysqld restart為例

那么mysqld為/etc/init.d/下面的一個(gè)可執(zhí)行文件,我們可以看到

[root@zejin240 chenzejin]# ll /etc/init.d/mysql -rwxr-xr-x. 1 root root 10815 Jan 14 2014 /etc/init.d/mysql

restart為參數(shù),將傳遞給mysqld腳本

這個(gè)命令在service執(zhí)行到后面最終調(diào)用的是:

env -i PATH="$PATH" TERM="$TERM" "${SERVICEDIR}/${SERVICE}" ${OPTIONS}

相當(dāng)于執(zhí)行了如下命令

/etc/init.d/mysqld restart

類似的,如果你的執(zhí)行命令為service mysqld stop start,那么就相當(dāng)于執(zhí)行了

/etc/init.d/mysqld stop start

至于命令執(zhí)行成不成功,就看你腳本支不支持多個(gè)參數(shù)的調(diào)用。

第二種理解:

service命令,顧名思義,就是用于管理Linux操作系統(tǒng)中服務(wù)的命令。

這個(gè)命令不是在所有的linux發(fā)行版本中都有。主要是在redhat、fedora、mandriva和centos中。

此命令位于/sbin目錄下,用file命令查看此命令會(huì)發(fā)現(xiàn)它是一個(gè)腳本命令。

分析腳本可知此命令的作用是去/etc/init.d目錄下尋找相應(yīng)的服務(wù),進(jìn)行開(kāi)啟和關(guān)閉等操作。

開(kāi)啟httpd服務(wù)器:service httpd start
start可以換成restart表示重新啟動(dòng),stop表示關(guān)閉,reload表示重新載入配置。

關(guān)閉mysql服務(wù)器:service mysqld stop

用法:

強(qiáng)烈建議大家將service命令替換為/etc/init.d/mysqld stop (因?yàn)橛幸恍﹍inux的版本不支持service)

Usage: service < option > | –status-all | [ service_name [ command | –full-restart ] ]

service oracle start   傳遞參數(shù)start
service oracle stop   傳遞參數(shù)為stop
service oracle restart   傳遞參數(shù)為restart
service oracle status  傳遞參數(shù)為status

service --status-all   顯示所有服務(wù)的狀態(tài)

service tomcat status 查看單個(gè)服務(wù)的狀態(tài)

相關(guān)知識(shí)

Firefox“瘦身”了,但只有Linux用戶能體驗(yàn)
Health Service Kit簡(jiǎn)介
503 Service Unavailable
Docker鏡像瘦身技巧的新思路(基于chiselled Ubuntu)本文介紹一種docker image瘦身的技巧
解碼微服務(wù)健康:如何精準(zhǔn)把握指標(biāo),確保系統(tǒng)穩(wěn)定運(yùn)行
負(fù)載均衡SLB四層(TCP/UDP)健康檢查出現(xiàn)異常的解決方法
Strategy of Fitness Qigong Integrating into and Promoting the Development of Health Service Industry in China
科學(xué)家解析節(jié)食延長(zhǎng)壽命原因
基于安卓平臺(tái)健康減肥軟件設(shè)計(jì)與開(kāi)發(fā).doc
Steam++

網(wǎng)址: linux service命令解析(重要) http://www.u1s5d6.cn/newsview906267.html

推薦資訊