Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.75 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ###########################################################################
  4. # /usr/bin/service
  5. #
  6. # A convenient wrapper for the /etc/init.d init scripts.
  7. #
  8. # This script is a modified version of the /sbin/service utility found on
  9. # Red Hat/Fedora systems (licensed GPLv2+).
  10. #
  11. # Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  12. # Copyright (C) 2008 Canonical Ltd.
  13. #   * August 2008 - Dustin Kirkland <kirkland@canonical.com>
  14. # Copyright (C) 2013 Michael Stapelberg <stapelberg@debian.org>
  15. #
  16. # This program is free software; you can redistribute it and/or modify
  17. # it under the terms of the GNU General Public License as published by
  18. # the Free Software Foundation; either version 2 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. # GNU General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU General Public License
  27. # along with this program; if not, write to the Free Software
  28. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  29. #
  30. # On Debian GNU/Linux systems, the complete text of the GNU General
  31. # Public License can be found in `/usr/share/common-licenses/GPL-2'.
  32. ###########################################################################
  33.  
  34.  
  35. is_ignored_file() {
  36.     case "$1" in
  37.         skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
  38.             return 0
  39.         ;;
  40.     esac
  41.     return 1
  42. }
  43.  
  44. VERSION="`basename $0` ver. 0.91-ubuntu1"
  45. USAGE="Usage: `basename $0` < option > | --status-all | \
  46. [ service_name [ command | --full-restart ] ]"
  47. SERVICE=
  48. ACTION=
  49. SERVICEDIR="/etc/init.d"
  50. OPTIONS=
  51. is_systemd=
  52.  
  53.  
  54. if [ $# -eq 0 ]; then
  55.    echo "${USAGE}" >&2
  56.    exit 1
  57. fi
  58.  
  59. if [ -d /run/systemd/system ]; then
  60.    is_systemd=1
  61. fi
  62.  
  63. cd /
  64. while [ $# -gt 0 ]; do
  65.   case "${1}" in
  66.     --help | -h | --h* )
  67.        echo "${USAGE}" >&2
  68.        exit 0
  69.        ;;
  70.     --version | -V )
  71.        echo "${VERSION}" >&2
  72.        exit 0
  73.        ;;
  74.     *)
  75.        if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
  76.           cd ${SERVICEDIR}
  77.           for SERVICE in * ; do
  78.             case "${SERVICE}" in
  79.               functions | halt | killall | single| linuxconf| kudzu)
  80.                   ;;
  81.               *)
  82.                 if ! is_ignored_file "${SERVICE}" \
  83.             && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  84.                         out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
  85.                         retval=$?
  86.                         if echo "$out" | egrep -iq "usage:"; then
  87.                           #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
  88.                           echo " [ ? ]  $SERVICE" 1>&2
  89.                           continue
  90.                         else
  91.                           if [ "$retval" = "0" -a -n "$out" ]; then
  92.                             #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
  93.                             echo " [ + ]  $SERVICE"
  94.                             continue
  95.                           else
  96.                             #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
  97.                             echo " [ - ]  $SERVICE"
  98.                             continue
  99.                           fi
  100.                         fi
  101.                   #env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status
  102.                 fi
  103.                 ;;
  104.             esac
  105.           done
  106.           exit 0
  107.        elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
  108.           SERVICE="${1}"
  109.           # On systems using systemd, we just perform a normal restart:
  110.           # A restart with systemd is already a full restart.
  111.           if [ -n "$is_systemd" ]; then
  112.              ACTION="restart"
  113.           else
  114.              if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  115.                env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" stop
  116.                env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" start
  117.                exit $?
  118.              fi
  119.           fi
  120.        elif [ -z "${SERVICE}" ]; then
  121.          SERVICE="${1}"
  122.        elif [ -z "${ACTION}" ]; then
  123.          ACTION="${1}"
  124.        else
  125.          OPTIONS="${OPTIONS} ${1}"
  126.        fi
  127.        shift
  128.        ;;
  129.    esac
  130. done
  131.  
  132. if [ -r "/etc/init/${SERVICE}.conf" ] && which initctl >/dev/null \
  133.    && initctl version 2>/dev/null | grep -q upstart
  134. then
  135.    # Upstart configuration exists for this job and we're running on upstart
  136.    case "${ACTION}" in
  137.       start|stop|status|reload)
  138.          # Action is a valid upstart action
  139.          exec ${ACTION} ${SERVICE} ${OPTIONS}
  140.       ;;
  141.       restart)
  142.         # Map restart to the usual sysvinit behavior.
  143.          stop ${SERVICE} ${OPTIONS} || :
  144.          exec start ${SERVICE} ${OPTIONS}
  145.       ;;
  146.       force-reload)
  147.          # Upstart just uses reload for force-reload
  148.          exec reload ${SERVICE} ${OPTIONS}
  149.       ;;
  150.    esac
  151. fi
  152.  
  153.  
  154. run_via_sysvinit() {
  155.    # Otherwise, use the traditional sysvinit
  156.    if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
  157.       exec env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" ${ACTION} ${OPTIONS}
  158.    else
  159.       echo "${SERVICE}: unrecognized service" >&2
  160.       exit 1
  161.    fi
  162. }
  163.  
  164. update_openrc_started_symlinks() {
  165.    # maintain the symlinks of /run/openrc/started so that
  166.    # rc-status works with the service command as well
  167.    if [ -d /run/openrc/started ] ; then
  168.       case "${ACTION}" in
  169.       start)
  170.          if [ ! -h /run/openrc/started/$SERVICE ] ; then
  171.             ln -s $SERVICEDIR/$SERVICE /run/openrc/started/$SERVICE || true
  172.          fi
  173.       ;;
  174.       stop)
  175.          rm /run/openrc/started/$SERVICE || true
  176.       ;;
  177.       esac
  178.    fi
  179. }
  180.  
  181. # When this machine is running systemd, standard service calls are turned into
  182. # systemctl calls.
  183. if [ -n "$is_systemd" ]
  184. then
  185.    UNIT="${SERVICE%.sh}.service"
  186.    case "${ACTION}" in
  187.       restart|status)
  188.          exec systemctl ${ACTION} ${UNIT}
  189.       ;;
  190.       start|stop)
  191.          # Follow the principle of least surprise for SysV people:
  192.          # When running "service foo stop" and foo happens to be a service that
  193.          # has one or more .socket files, we also stop the .socket units.
  194.          # Users who need more control will use systemctl directly.
  195.          for unit in $(systemctl list-unit-files --full --type=socket 2>/dev/null | sed -ne 's/\.socket\s*[a-z]*\s*$/.socket/p'); do
  196.              if [ "$(systemctl -p Triggers show $unit)" = "Triggers=${UNIT}" ]; then
  197.                 systemctl ${ACTION} $unit
  198.              fi
  199.          done
  200.          exec systemctl ${ACTION} ${UNIT}
  201.       ;;
  202.       reload)
  203.          _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
  204.          if [ "$_canreload" = "CanReload=no" ]; then
  205.             # The reload action falls back to the sysv init script just in case
  206.             # the systemd service file does not (yet) support reload for a
  207.             # specific service.
  208.             run_via_sysvinit
  209.          else
  210.             exec systemctl reload "${UNIT}"
  211.          fi
  212.          ;;
  213.       force-stop)
  214.          exec systemctl --signal=KILL kill "${UNIT}"
  215.          ;;
  216.       force-reload)
  217.          _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
  218.          if [ "$_canreload" = "CanReload=no" ]; then
  219.             exec systemctl restart "${UNIT}"
  220.          else
  221.             exec systemctl reload "${UNIT}"
  222.          fi
  223.          ;;
  224.       *)
  225.          # We try to run non-standard actions by running
  226.          # the init script directly.
  227.          run_via_sysvinit
  228.          ;;
  229.    esac
  230. fi
  231.  
  232. update_openrc_started_symlinks
  233. run_via_sysvinit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement