Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1754266
  • 博文数量: 413
  • 博客积分: 8399
  • 博客等级: 中将
  • 技术积分: 4325
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 10:44
文章分类

全部博文(413)

文章存档

2015年(1)

2014年(18)

2013年(39)

2012年(163)

2011年(192)

分类: LINUX

2011-11-18 20:44:50

我们知道service命令是用于管理Linux系统中的各种服务的命令。其实service是位于系统中/usr/sbin/service的一个可执行的脚本,其内容如下:
  1. #!/bin/sh
  2. ###########################################################################
  3. # /usr/bin/service
  4. #
  5. # A convenient wrapper for the /etc/init.d init scripts. 
  6. #(不过是/etc/init.d/ 目录下各种可执行脚本的一个为了方便调用而增加的一个包装而已。)
  7. #
  8. ###########################################################################
  9. is_ignored_file() {
  10.         case "$1" in
  11.                 skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
  12.                         return 0
  13.                 ;;
  14.         esac
  15.         return 1
  16. }

  17. VERSION="`basename $0` ver. 0.91-ubuntu1"
  18. USAGE="Usage: `basename $0` < option > | --status-all | \
  19. [ service_name [ command | --full-restart ] ]"
  20. SERVICE=
  21. ACTION=
  22. SERVICEDIR="/etc/init.d"
  23. OPTIONS=
  24. # 没有给出参数,打印提示信息
  25. if [ $# -eq 0 ]; then
  26.    echo "${USAGE}" >&2
  27.    exit 1
  28. fi
  29. # 改变目录
  30. cd /
  31. while [ $# -gt 0 ]; do
  32.   case "${1}" in           
  33.     --help | -h | --h* )     # 处理 service --help / -h / --h
  34.        echo "${USAGE}" >&2
  35.        exit 0
  36.        ;;
  37.     --version | -V )         # 处理 service --version / -V 
  38.        echo "${VERSION}" >&2
  39.        exit 0
  40.        ;;
  41.     *)                       # 处理 其它的情况
  42.        # 没有命令名,并且参数个数为1,并且此参数为 --status-all. 也就是 "service --status-all"
  43.        if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
  44.           cd ${SERVICEDIR}          # 进入目录/etc/init.d/
  45.           for SERVICE in * ; do     # 对目录/etc/init.d/中的每一个文件进行下面的处理 
  46.             case "${SERVICE}" in    # 忽略下面一些的文件的执行
  47.               functions | halt | killall | single| linuxconf| kudzu)
  48.                   ;;
  49.               *)  # 其它不可忽略的情况的处理
  50.                   # 文件不是可以忽略的,并且还具有可执行权限
  51.                 if ! is_ignored_file "${SERVICE}" \
  52.                     && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  53.                         if ! grep -qs "\Wstatus)" "$SERVICE"; then
  54.                           #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
  55.                           echo " [ ? ] $SERVICE" 1>&2
  56.                           continue
  57.                         else    # 获得对应命令的status
  58.                           out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)    
  59.                         #上面的env -i正确执行完成,并且执行的结果$out非空(true),说明对应的命令正在运行
  60.                           if [ "$?" = "0" -a -n "$out" ]; then
  61.                             #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
  62.                             echo " [ + ] $SERVICE"
  63.                             continue
  64.                           else # 对应的命令没有正在运行
  65.                             #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
  66.                             echo " [ - ] $SERVICE"
  67.                             continue
  68.                           fi
  69.                         fi
  70.                   #env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status
  71.                 fi
  72.                 ;;
  73.             esac
  74.           done
  75.           exit 0
  76.        # 如果参数的个数位2,并且第二个参数为--full-restart
  77.        elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
  78.           SERVICE="${1}"   # 先停止,然后开启
  79.           if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  80.             env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" stop
  81.             env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" start
  82.             exit $?
  83.           fi
  84.        elif [ -z "${SERVICE}" ]; then    # 给在开头定义的变量赋值
  85.          SERVICE="${1}"
  86.        elif [ -z "${ACTION}" ]; then     # 给在开头定义的变量赋值
  87.          ACTION="${1}"
  88.        else
  89.          OPTIONS="${OPTIONS} ${1}"       # 给在开头定义的变量赋值
  90.        fi
  91.        shift
  92.        ;;
  93.    esac
  94. done

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

 # env -i 删除所以的环境变量,仅仅保留LANG PATH TERM三个环境变量,然后执行安装参数执行命令
  1. # Otherwise, use the traditional sysvinit
  2. if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  3.    exec env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" ${ACTION} ${OPTIONS}
  4. else
  5.    echo "${SERVICE}: unrecognized service" >&2
  6.    exit 1
  7. fi
从上面的脚本的分析可以知道:
service的本质是一个可执行的脚本,它的功能“不过是/etc/init.d/ 目录下各种可执行脚本的一个为了方便调用而增加的一个包装而已。”

下面是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
......

阅读(8821) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~