码迷,mamicode.com
首页 > 系统相关 > 详细

用于管理Linux系统中的各种服务的命令service命令

时间:2015-03-30 13:02:23      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

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

 # 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

用于管理Linux系统中的各种服务的命令service命令

标签:

原文地址:http://www.cnblogs.com/w-20150101/p/4377553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!