學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > linux service命令詳解

linux service命令詳解

時(shí)間: 佳洲1085 分享

linux service命令詳解

  service命令是linux系統(tǒng)下的一個(gè)命令,那它有什么用處呢?下面由學(xué)習(xí)啦小編為大家整理了linux系統(tǒng)下service命令的相關(guān)知識(shí),希望大家喜歡!

  linux service命令詳解

  用途說(shuō)明

  service命令用于對(duì)系統(tǒng)服務(wù)進(jìn)行管理,比如啟動(dòng)(start)、停止(stop)、重啟(restart)、查看狀態(tài)(status)等。相關(guān)的命令還包括chkconfig、ntsysv等,chkconfig用于查看、設(shè)置服務(wù)的運(yùn)行級(jí)別,ntsysv用于直觀方便的設(shè)置各個(gè)服務(wù)是否自動(dòng)啟動(dòng)。service命令本身是一個(gè)shell腳本,它在/etc/init.d/目錄查找指定的服務(wù)腳本,然后調(diào)用該服務(wù)腳本來(lái)完成任務(wù)。

  看看下面的手冊(cè)頁(yè)可能更加清楚的了解service的內(nèi)幕:service運(yùn)行指定服務(wù)(稱(chēng)之為System V初始腳本)時(shí),把大部分環(huán)境變量去掉了,只保留LANG和TERM兩個(gè)環(huán)境變量,并且把當(dāng)前路徑置為/,也就是說(shuō)是在一個(gè)可以預(yù)測(cè)的非常干凈的環(huán)境中運(yùn)行服務(wù)腳本。這種腳本保存在/etc/init.d目錄中,它至少要支持start和stop命令。

  man service 寫(xiě)道

  service(8) service(8)

  NAME

  service - run a System V init script

  SYNOPSIS

  service SCRIPT COMMAND [OPTIONS]

  service --status-all

  service --help | -h | --version

  DESCRIPTION

  service runs a System V init script in as predictable environment as possible, removing most environment vari-

  ables and with current working directory set to /.

  The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT. The supported values of

  COMMAND depend on the invoked script, service passes COMMAND and OPTIONS it to the init script unmodified. All

  scripts should support at least the start and stop commands. As a special case, if COMMAND is --full-restart,

  the script is run twice, first with the stop command, then with the start command.

  service --status-all runs all init scripts, in alphabetical order, with the status command.

  FILES

  /etc/init.d

  The directory containing System V init scripts.

  ENVIRONMENT

  LANG, TERM

  The only environment variables passed to the init scripts.

  SEE ALSO

  chkconfig(8), ntsysv(8)

  Jan 2006 service(8)

  常用方式

  格式:service

  打印指定服務(wù)的命令行使用幫助。

  格式:service start

  啟動(dòng)指定的系統(tǒng)服務(wù)

  格式:service stop

  停止指定的系統(tǒng)服務(wù)

  格式:service restart

  重新啟動(dòng)指定的系統(tǒng)服務(wù),即先停止(stop),然后再啟動(dòng)(start)。

  格式:chkconfig --list

  查看系統(tǒng)服務(wù)列表,以及每個(gè)服務(wù)的運(yùn)行級(jí)別。

  格式:chkconfig on

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)自動(dòng)啟動(dòng)。

  格式:chkconfig off

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)不自動(dòng)啟動(dòng)。

  格式:ntsysv

  以全屏幕文本界面設(shè)置服務(wù)開(kāi)機(jī)時(shí)是否自動(dòng)啟動(dòng)。

  使用示例

  示例一 網(wǎng)絡(luò)重啟

  當(dāng)修改了主機(jī)名、ip地址等信息時(shí),經(jīng)常需要把網(wǎng)絡(luò)重啟使之生效。

  [root@node34 root]# service network

  用法:/etc/init.d/network {start|stop|restart|reload|status}

  [root@node34 root]# service network status

  配置設(shè)備:

  lo eth0

  當(dāng)前的活躍設(shè)備:

  lo eth0

  [root@node34 root]# service network restart

  正在關(guān)閉接口 eth0: [ 確定 ]

  關(guān)閉環(huán)回接口: [ 確定 ]

  設(shè)置網(wǎng)絡(luò)參數(shù): [ 確定 ]

  彈出環(huán)回接口: [ 確定 ]

  彈出界面 eth0: [ 確定 ]

  [root@node34 root]#

  示例二 重啟MySQL

  [root@node34 root]# service mysql

  mysql: unrecognized service

  [root@node34 root]# service mysqld

  用法:/etc/init.d/mysqld {start|stop|status|condrestart|restart}

  [root@node34 root]# service mysqld status

  mysqld (pid 1638) 正在運(yùn)行...

  [root@node34 root]# service mysqld restart

  停止 MySQL: [ 確定 ]

  啟動(dòng) MySQL: [ 確定 ]

  [root@node34 root]#

  示例三 service腳本源碼展示

  [root@web ~]# cat /sbin/service

  #!/bin/sh

  . /etc/init.d/functions

  VERSION="`basename

linux service命令詳解

時(shí)間: 佳洲1085 分享

linux service命令詳解

  service命令是linux系統(tǒng)下的一個(gè)命令,那它有什么用處呢?下面由學(xué)習(xí)啦小編為大家整理了linux系統(tǒng)下service命令的相關(guān)知識(shí),希望大家喜歡!

  linux service命令詳解

  用途說(shuō)明

  service命令用于對(duì)系統(tǒng)服務(wù)進(jìn)行管理,比如啟動(dòng)(start)、停止(stop)、重啟(restart)、查看狀態(tài)(status)等。相關(guān)的命令還包括chkconfig、ntsysv等,chkconfig用于查看、設(shè)置服務(wù)的運(yùn)行級(jí)別,ntsysv用于直觀方便的設(shè)置各個(gè)服務(wù)是否自動(dòng)啟動(dòng)。service命令本身是一個(gè)shell腳本,它在/etc/init.d/目錄查找指定的服務(wù)腳本,然后調(diào)用該服務(wù)腳本來(lái)完成任務(wù)。

  看看下面的手冊(cè)頁(yè)可能更加清楚的了解service的內(nèi)幕:service運(yùn)行指定服務(wù)(稱(chēng)之為System V初始腳本)時(shí),把大部分環(huán)境變量去掉了,只保留LANG和TERM兩個(gè)環(huán)境變量,并且把當(dāng)前路徑置為/,也就是說(shuō)是在一個(gè)可以預(yù)測(cè)的非常干凈的環(huán)境中運(yùn)行服務(wù)腳本。這種腳本保存在/etc/init.d目錄中,它至少要支持start和stop命令。

  man service 寫(xiě)道

  service(8) service(8)

  NAME

  service - run a System V init script

  SYNOPSIS

  service SCRIPT COMMAND [OPTIONS]

  service --status-all

  service --help | -h | --version

  DESCRIPTION

  service runs a System V init script in as predictable environment as possible, removing most environment vari-

  ables and with current working directory set to /.

  The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT. The supported values of

  COMMAND depend on the invoked script, service passes COMMAND and OPTIONS it to the init script unmodified. All

  scripts should support at least the start and stop commands. As a special case, if COMMAND is --full-restart,

  the script is run twice, first with the stop command, then with the start command.

  service --status-all runs all init scripts, in alphabetical order, with the status command.

  FILES

  /etc/init.d

  The directory containing System V init scripts.

  ENVIRONMENT

  LANG, TERM

  The only environment variables passed to the init scripts.

  SEE ALSO

  chkconfig(8), ntsysv(8)

  Jan 2006 service(8)

  常用方式

  格式:service

  打印指定服務(wù)的命令行使用幫助。

  格式:service start

  啟動(dòng)指定的系統(tǒng)服務(wù)

  格式:service stop

  停止指定的系統(tǒng)服務(wù)

  格式:service restart

  重新啟動(dòng)指定的系統(tǒng)服務(wù),即先停止(stop),然后再啟動(dòng)(start)。

  格式:chkconfig --list

  查看系統(tǒng)服務(wù)列表,以及每個(gè)服務(wù)的運(yùn)行級(jí)別。

  格式:chkconfig on

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)自動(dòng)啟動(dòng)。

  格式:chkconfig off

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)不自動(dòng)啟動(dòng)。

  格式:ntsysv

  以全屏幕文本界面設(shè)置服務(wù)開(kāi)機(jī)時(shí)是否自動(dòng)啟動(dòng)。

  使用示例

  示例一 網(wǎng)絡(luò)重啟

  當(dāng)修改了主機(jī)名、ip地址等信息時(shí),經(jīng)常需要把網(wǎng)絡(luò)重啟使之生效。

  [root@node34 root]# service network

  用法:/etc/init.d/network {start|stop|restart|reload|status}

  [root@node34 root]# service network status

  配置設(shè)備:

  lo eth0

  當(dāng)前的活躍設(shè)備:

  lo eth0

  [root@node34 root]# service network restart

  正在關(guān)閉接口 eth0: [ 確定 ]

  關(guān)閉環(huán)回接口: [ 確定 ]

  設(shè)置網(wǎng)絡(luò)參數(shù): [ 確定 ]

  彈出環(huán)回接口: [ 確定 ]

  彈出界面 eth0: [ 確定 ]

  [root@node34 root]#

  示例二 重啟MySQL

  [root@node34 root]# service mysql

  mysql: unrecognized service

  [root@node34 root]# service mysqld

  用法:/etc/init.d/mysqld {start|stop|status|condrestart|restart}

  [root@node34 root]# service mysqld status

  mysqld (pid 1638) 正在運(yùn)行...

  [root@node34 root]# service mysqld restart

  停止 MySQL: [ 確定 ]

  啟動(dòng) MySQL: [ 確定 ]

  [root@node34 root]#

  示例三 service腳本源碼展示

  [root@web ~]# cat /sbin/service

  #!/bin/sh

  . /etc/init.d/functions

  VERSION="`basename

` ver. 0.91"

  USAGE="Usage: `basename

linux service命令詳解

時(shí)間: 佳洲1085 分享

linux service命令詳解

  service命令是linux系統(tǒng)下的一個(gè)命令,那它有什么用處呢?下面由學(xué)習(xí)啦小編為大家整理了linux系統(tǒng)下service命令的相關(guān)知識(shí),希望大家喜歡!

  linux service命令詳解

  用途說(shuō)明

  service命令用于對(duì)系統(tǒng)服務(wù)進(jìn)行管理,比如啟動(dòng)(start)、停止(stop)、重啟(restart)、查看狀態(tài)(status)等。相關(guān)的命令還包括chkconfig、ntsysv等,chkconfig用于查看、設(shè)置服務(wù)的運(yùn)行級(jí)別,ntsysv用于直觀方便的設(shè)置各個(gè)服務(wù)是否自動(dòng)啟動(dòng)。service命令本身是一個(gè)shell腳本,它在/etc/init.d/目錄查找指定的服務(wù)腳本,然后調(diào)用該服務(wù)腳本來(lái)完成任務(wù)。

  看看下面的手冊(cè)頁(yè)可能更加清楚的了解service的內(nèi)幕:service運(yùn)行指定服務(wù)(稱(chēng)之為System V初始腳本)時(shí),把大部分環(huán)境變量去掉了,只保留LANG和TERM兩個(gè)環(huán)境變量,并且把當(dāng)前路徑置為/,也就是說(shuō)是在一個(gè)可以預(yù)測(cè)的非常干凈的環(huán)境中運(yùn)行服務(wù)腳本。這種腳本保存在/etc/init.d目錄中,它至少要支持start和stop命令。

  man service 寫(xiě)道

  service(8) service(8)

  NAME

  service - run a System V init script

  SYNOPSIS

  service SCRIPT COMMAND [OPTIONS]

  service --status-all

  service --help | -h | --version

  DESCRIPTION

  service runs a System V init script in as predictable environment as possible, removing most environment vari-

  ables and with current working directory set to /.

  The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT. The supported values of

  COMMAND depend on the invoked script, service passes COMMAND and OPTIONS it to the init script unmodified. All

  scripts should support at least the start and stop commands. As a special case, if COMMAND is --full-restart,

  the script is run twice, first with the stop command, then with the start command.

  service --status-all runs all init scripts, in alphabetical order, with the status command.

  FILES

  /etc/init.d

  The directory containing System V init scripts.

  ENVIRONMENT

  LANG, TERM

  The only environment variables passed to the init scripts.

  SEE ALSO

  chkconfig(8), ntsysv(8)

  Jan 2006 service(8)

  常用方式

  格式:service

  打印指定服務(wù)的命令行使用幫助。

  格式:service start

  啟動(dòng)指定的系統(tǒng)服務(wù)

  格式:service stop

  停止指定的系統(tǒng)服務(wù)

  格式:service restart

  重新啟動(dòng)指定的系統(tǒng)服務(wù),即先停止(stop),然后再啟動(dòng)(start)。

  格式:chkconfig --list

  查看系統(tǒng)服務(wù)列表,以及每個(gè)服務(wù)的運(yùn)行級(jí)別。

  格式:chkconfig on

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)自動(dòng)啟動(dòng)。

  格式:chkconfig off

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)不自動(dòng)啟動(dòng)。

  格式:ntsysv

  以全屏幕文本界面設(shè)置服務(wù)開(kāi)機(jī)時(shí)是否自動(dòng)啟動(dòng)。

  使用示例

  示例一 網(wǎng)絡(luò)重啟

  當(dāng)修改了主機(jī)名、ip地址等信息時(shí),經(jīng)常需要把網(wǎng)絡(luò)重啟使之生效。

  [root@node34 root]# service network

  用法:/etc/init.d/network {start|stop|restart|reload|status}

  [root@node34 root]# service network status

  配置設(shè)備:

  lo eth0

  當(dāng)前的活躍設(shè)備:

  lo eth0

  [root@node34 root]# service network restart

  正在關(guān)閉接口 eth0: [ 確定 ]

  關(guān)閉環(huán)回接口: [ 確定 ]

  設(shè)置網(wǎng)絡(luò)參數(shù): [ 確定 ]

  彈出環(huán)回接口: [ 確定 ]

  彈出界面 eth0: [ 確定 ]

  [root@node34 root]#

  示例二 重啟MySQL

  [root@node34 root]# service mysql

  mysql: unrecognized service

  [root@node34 root]# service mysqld

  用法:/etc/init.d/mysqld {start|stop|status|condrestart|restart}

  [root@node34 root]# service mysqld status

  mysqld (pid 1638) 正在運(yùn)行...

  [root@node34 root]# service mysqld restart

  停止 MySQL: [ 確定 ]

  啟動(dòng) MySQL: [ 確定 ]

  [root@node34 root]#

  示例三 service腳本源碼展示

  [root@web ~]# cat /sbin/service

  #!/bin/sh

  . /etc/init.d/functions

  VERSION="`basename

` < 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 " class="main">

linux service命令詳解

時(shí)間: 佳洲1085 分享

  --help | -h | --h* )

  echo "${USAGE}" >&2

  exit 0

  ;;

  --version | -V )

  echo "${VERSION}" >&2

  exit 0

  ;;

  *)

  if [ -z "${SERVICE}" -a $# -eq 1 -a " class="main">

linux service命令詳解

時(shí)間: 佳洲1085 分享

  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 "

linux service命令詳解

  service命令是linux系統(tǒng)下的一個(gè)命令,那它有什么用處呢?下面由學(xué)習(xí)啦小編為大家整理了linux系統(tǒng)下service命令的相關(guān)知識(shí),希望大家喜歡!

  linux service命令詳解

  用途說(shuō)明

  service命令用于對(duì)系統(tǒng)服務(wù)進(jìn)行管理,比如啟動(dòng)(start)、停止(stop)、重啟(restart)、查看狀態(tài)(status)等。相關(guān)的命令還包括chkconfig、ntsysv等,chkconfig用于查看、設(shè)置服務(wù)的運(yùn)行級(jí)別,ntsysv用于直觀方便的設(shè)置各個(gè)服務(wù)是否自動(dòng)啟動(dòng)。service命令本身是一個(gè)shell腳本,它在/etc/init.d/目錄查找指定的服務(wù)腳本,然后調(diào)用該服務(wù)腳本來(lái)完成任務(wù)。

  看看下面的手冊(cè)頁(yè)可能更加清楚的了解service的內(nèi)幕:service運(yùn)行指定服務(wù)(稱(chēng)之為System V初始腳本)時(shí),把大部分環(huán)境變量去掉了,只保留LANG和TERM兩個(gè)環(huán)境變量,并且把當(dāng)前路徑置為/,也就是說(shuō)是在一個(gè)可以預(yù)測(cè)的非常干凈的環(huán)境中運(yùn)行服務(wù)腳本。這種腳本保存在/etc/init.d目錄中,它至少要支持start和stop命令。

  man service 寫(xiě)道

  service(8) service(8)

  NAME

  service - run a System V init script

  SYNOPSIS

  service SCRIPT COMMAND [OPTIONS]

  service --status-all

  service --help | -h | --version

  DESCRIPTION

  service runs a System V init script in as predictable environment as possible, removing most environment vari-

  ables and with current working directory set to /.

  The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT. The supported values of

  COMMAND depend on the invoked script, service passes COMMAND and OPTIONS it to the init script unmodified. All

  scripts should support at least the start and stop commands. As a special case, if COMMAND is --full-restart,

  the script is run twice, first with the stop command, then with the start command.

  service --status-all runs all init scripts, in alphabetical order, with the status command.

  FILES

  /etc/init.d

  The directory containing System V init scripts.

  ENVIRONMENT

  LANG, TERM

  The only environment variables passed to the init scripts.

  SEE ALSO

  chkconfig(8), ntsysv(8)

  Jan 2006 service(8)

  常用方式

  格式:service

  打印指定服務(wù)的命令行使用幫助。

  格式:service start

  啟動(dòng)指定的系統(tǒng)服務(wù)

  格式:service stop

  停止指定的系統(tǒng)服務(wù)

  格式:service restart

  重新啟動(dòng)指定的系統(tǒng)服務(wù),即先停止(stop),然后再啟動(dòng)(start)。

  格式:chkconfig --list

  查看系統(tǒng)服務(wù)列表,以及每個(gè)服務(wù)的運(yùn)行級(jí)別。

  格式:chkconfig on

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)自動(dòng)啟動(dòng)。

  格式:chkconfig off

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)不自動(dòng)啟動(dòng)。

  格式:ntsysv

  以全屏幕文本界面設(shè)置服務(wù)開(kāi)機(jī)時(shí)是否自動(dòng)啟動(dòng)。

  使用示例

  示例一 網(wǎng)絡(luò)重啟

  當(dāng)修改了主機(jī)名、ip地址等信息時(shí),經(jīng)常需要把網(wǎng)絡(luò)重啟使之生效。

  [root@node34 root]# service network

  用法:/etc/init.d/network {start|stop|restart|reload|status}

  [root@node34 root]# service network status

  配置設(shè)備:

  lo eth0

  當(dāng)前的活躍設(shè)備:

  lo eth0

  [root@node34 root]# service network restart

  正在關(guān)閉接口 eth0: [ 確定 ]

  關(guān)閉環(huán)回接口: [ 確定 ]

  設(shè)置網(wǎng)絡(luò)參數(shù): [ 確定 ]

  彈出環(huán)回接口: [ 確定 ]

  彈出界面 eth0: [ 確定 ]

  [root@node34 root]#

  示例二 重啟MySQL

  [root@node34 root]# service mysql

  mysql: unrecognized service

  [root@node34 root]# service mysqld

  用法:/etc/init.d/mysqld {start|stop|status|condrestart|restart}

  [root@node34 root]# service mysqld status

  mysqld (pid 1638) 正在運(yùn)行...

  [root@node34 root]# service mysqld restart

  停止 MySQL: [ 確定 ]

  啟動(dòng) MySQL: [ 確定 ]

  [root@node34 root]#

  示例三 service腳本源碼展示

  [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 ~]#

  示例四 crond服務(wù)的源碼

  [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]#

" = "--full-restart" ]; then

  SERVICE=" class="main">

linux service命令詳解

時(shí)間: 佳洲1085 分享

  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=" class="main">

linux service命令詳解

時(shí)間: 佳洲1085 分享

  else

  OPTIONS="${OPTIONS} class="main">

linux service命令詳解

時(shí)間: 佳洲1085 分享

  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 ~]#

  示例四 crond服務(wù)的源碼

  [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 " class="main">

linux service命令詳解

時(shí)間: 佳洲1085 分享

  start)

  start

  ;;

  stop)

  stop

  ;;

  restart)

  restart

  ;;

  reload)

  reload

  ;;

  status)

  rhstatus

  ;;

  condrestart)

  [ -f /var/lock/subsys/crond ] && restart || :

  ;;

  *)

  echo $"Usage:

linux service命令詳解

時(shí)間: 佳洲1085 分享

linux service命令詳解

  service命令是linux系統(tǒng)下的一個(gè)命令,那它有什么用處呢?下面由學(xué)習(xí)啦小編為大家整理了linux系統(tǒng)下service命令的相關(guān)知識(shí),希望大家喜歡!

  linux service命令詳解

  用途說(shuō)明

  service命令用于對(duì)系統(tǒng)服務(wù)進(jìn)行管理,比如啟動(dòng)(start)、停止(stop)、重啟(restart)、查看狀態(tài)(status)等。相關(guān)的命令還包括chkconfig、ntsysv等,chkconfig用于查看、設(shè)置服務(wù)的運(yùn)行級(jí)別,ntsysv用于直觀方便的設(shè)置各個(gè)服務(wù)是否自動(dòng)啟動(dòng)。service命令本身是一個(gè)shell腳本,它在/etc/init.d/目錄查找指定的服務(wù)腳本,然后調(diào)用該服務(wù)腳本來(lái)完成任務(wù)。

  看看下面的手冊(cè)頁(yè)可能更加清楚的了解service的內(nèi)幕:service運(yùn)行指定服務(wù)(稱(chēng)之為System V初始腳本)時(shí),把大部分環(huán)境變量去掉了,只保留LANG和TERM兩個(gè)環(huán)境變量,并且把當(dāng)前路徑置為/,也就是說(shuō)是在一個(gè)可以預(yù)測(cè)的非常干凈的環(huán)境中運(yùn)行服務(wù)腳本。這種腳本保存在/etc/init.d目錄中,它至少要支持start和stop命令。

  man service 寫(xiě)道

  service(8) service(8)

  NAME

  service - run a System V init script

  SYNOPSIS

  service SCRIPT COMMAND [OPTIONS]

  service --status-all

  service --help | -h | --version

  DESCRIPTION

  service runs a System V init script in as predictable environment as possible, removing most environment vari-

  ables and with current working directory set to /.

  The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT. The supported values of

  COMMAND depend on the invoked script, service passes COMMAND and OPTIONS it to the init script unmodified. All

  scripts should support at least the start and stop commands. As a special case, if COMMAND is --full-restart,

  the script is run twice, first with the stop command, then with the start command.

  service --status-all runs all init scripts, in alphabetical order, with the status command.

  FILES

  /etc/init.d

  The directory containing System V init scripts.

  ENVIRONMENT

  LANG, TERM

  The only environment variables passed to the init scripts.

  SEE ALSO

  chkconfig(8), ntsysv(8)

  Jan 2006 service(8)

  常用方式

  格式:service

  打印指定服務(wù)的命令行使用幫助。

  格式:service start

  啟動(dòng)指定的系統(tǒng)服務(wù)

  格式:service stop

  停止指定的系統(tǒng)服務(wù)

  格式:service restart

  重新啟動(dòng)指定的系統(tǒng)服務(wù),即先停止(stop),然后再啟動(dòng)(start)。

  格式:chkconfig --list

  查看系統(tǒng)服務(wù)列表,以及每個(gè)服務(wù)的運(yùn)行級(jí)別。

  格式:chkconfig on

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)自動(dòng)啟動(dòng)。

  格式:chkconfig off

  設(shè)置指定服務(wù)開(kāi)機(jī)時(shí)不自動(dòng)啟動(dòng)。

  格式:ntsysv

  以全屏幕文本界面設(shè)置服務(wù)開(kāi)機(jī)時(shí)是否自動(dòng)啟動(dòng)。

  使用示例

  示例一 網(wǎng)絡(luò)重啟

  當(dāng)修改了主機(jī)名、ip地址等信息時(shí),經(jīng)常需要把網(wǎng)絡(luò)重啟使之生效。

  [root@node34 root]# service network

  用法:/etc/init.d/network {start|stop|restart|reload|status}

  [root@node34 root]# service network status

  配置設(shè)備:

  lo eth0

  當(dāng)前的活躍設(shè)備:

  lo eth0

  [root@node34 root]# service network restart

  正在關(guān)閉接口 eth0: [ 確定 ]

  關(guān)閉環(huán)回接口: [ 確定 ]

  設(shè)置網(wǎng)絡(luò)參數(shù): [ 確定 ]

  彈出環(huán)回接口: [ 確定 ]

  彈出界面 eth0: [ 確定 ]

  [root@node34 root]#

  示例二 重啟MySQL

  [root@node34 root]# service mysql

  mysql: unrecognized service

  [root@node34 root]# service mysqld

  用法:/etc/init.d/mysqld {start|stop|status|condrestart|restart}

  [root@node34 root]# service mysqld status

  mysqld (pid 1638) 正在運(yùn)行...

  [root@node34 root]# service mysqld restart

  停止 MySQL: [ 確定 ]

  啟動(dòng) MySQL: [ 確定 ]

  [root@node34 root]#

  示例三 service腳本源碼展示

  [root@web ~]# cat /sbin/service

  #!/bin/sh

  . /etc/init.d/functions

  VERSION="`basename

{start|stop|status|reload|restart|condrestart}"

  exit 1

  esac

  [root@web init.d]#

3573967