我们知道service命令是用于管理Linux系统中的各种服务的命令。其实service是位于系统中/usr/sbin/service的一个可执行的脚本,其内容如下:

#!/bin/sh
###########################################################################
# /usr/bin/service
#
# A convenient wrapper for the /etc/init.d init scripts. 
#(不过是/etc/init.d/ 目录下各种可执行脚本的一个为了方便调用而增加的一个包装而已。)
#
###########################################################################
is_ignored_file() {
        case "$1" in
                skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
                        return 0
                ;;
        esac
        return 1
}

VERSION="`basename $0` ver. 0.91-ubuntu1"
USAGE="Usage: `basename $0` < option > | --status-all | \
[ service_name [ command | --full-restart ] ]"
SERVICE=
ACTION=
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* )     # 处理 service --help / -h / --h
       echo "${USAGE}" >&2
       exit 0
       ;;
    --version | -V )         # 处理 service --version / -V 
       echo "${VERSION}" >&2
       exit 0
       ;;
    *)                       # 处理 其它的情况
       # 没有命令名,并且参数个数为1,并且此参数为 --status-all. 也就是 "service --status-all"
       if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
          cd ${SERVICEDIR}          # 进入目录/etc/init.d/
          for SERVICE in * ; do     # 对目录/etc/init.d/中的每一个文件进行下面的处理 
            case "${SERVICE}" in    # 忽略下面一些的文件的执行
              functions | halt | killall | single| linuxconf| kudzu)
                  ;;
              *)  # 其它不可忽略的情况的处理
                  # 文件不是可以忽略的,并且还具有可执行权限
                if ! is_ignored_file "${SERVICE}" \
                    && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
                        if ! grep -qs "\Wstatus)" "$SERVICE"; then
                          #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
                          echo " [ ? ] $SERVICE" 1>&2
                          continue
                        else    # 获得对应命令的status
                          out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)    
                        #上面的env -i正确执行完成,并且执行的结果$out非空(true),说明对应的命令正在运行
                          if [ "$?" = "0" -a -n "$out" ]; then
                            #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
                            echo " [ + ] $SERVICE"
                            continue
                          else # 对应的命令没有正在运行
                            #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
                            echo " [ - ] $SERVICE"
                            continue
                          fi
                        fi
                  #env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status
                fi
                ;;
            esac
          done
          exit 0
       # 如果参数的个数位2,并且第二个参数为--full-restart
       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}"
       elif [ -z "${ACTION}" ]; then     # 给在开头定义的变量赋值
         ACTION="${1}"
       else
         OPTIONS="${OPTIONS} ${1}"       # 给在开头定义的变量赋值
       fi
       shift
       ;;
   esac
done

# 判断是否存在对应命令的配置文件
if [ -r "/etc/init/${SERVICE}.conf" ]; then
   # Upstart configuration exists for this job
   case "${ACTION}" in
      start|stop|restart|status|reload)
         # Action is a valid upstart action
         exec ${ACTION} ${SERVICE} ${OPTIONS}    # 根据参数执行
      ;;
      force-reload)
         # Upstart just uses reload for force-reload
         exec reload ${SERVICE} ${OPTIONS}       # 根据参数执行
      ;;
   esac
fi

 # env -i 删除所以的环境变量,仅仅保留LANG PATH TERM三个环境变量,然后执行安装参数执行命令
# Otherwise, use the traditional sysvinit
if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
   exec env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" ${ACTION} ${OPTIONS}
else
   echo "${SERVICE}: unrecognized service" >&2
   exit 1
fi
下面是man service的结果:
 
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
       variables 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.
 
EXIT CODES
       service calls the init script and returns the status returned by it.
FILES
       /etc/init.d
              The directory containing System V init scripts.
ENVIRONMENT
       LANG, TERM
              The only environment variables passed to the init scripts.
 
从man 8 service的结果我们知道:
1. service的格式:service SCRIPT COMMAND [OPTIONS]
    也就是: service    脚本名      命令(stop/start/restart)     命令选项
2.  /etc/init.d/目录下的脚本至少要提供的两条命令: stop、start
3. service 调用的命令的执行环境是可预测的,一般只有两到三个环境变量(LANG, TERM, PATH).
 
经常使用的一些例子:
sudo service gdm stop
sudo service gdm start
sudo service gdm restart
service gdm status
 
sudo service networking stop
sudo service networking start
sudo service networking restart
sudo service networking status
 
sudo service mysqld restart
.......
sudo service apache2 restart
posted on 2015-12-11 12:33  裴银祥的博客园  阅读(1280)  评论(0编辑  收藏  举报