Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.50 KB | None | 0 0
  1. #!/bin/sh -e
  2. # upstart-job
  3. #
  4. # Symlink target for initscripts that have been converted to Upstart.
  5.  
  6. set -e
  7.  
  8. INITSCRIPT="$(basename "$0")"
  9. JOB="${INITSCRIPT%.sh}"
  10.  
  11. if [ "$JOB" = "upstart-job" ]; then
  12.     if [ -z "$1" ]; then
  13.         echo "Usage: upstart-job JOB COMMAND" 1>&2
  14.         exit 1
  15.     fi
  16.  
  17.     JOB="$1"
  18.     INITSCRIPT="$1"
  19.     shift
  20. else
  21.     if [ -z "$1" ]; then
  22.         echo "Usage: $0 COMMAND" 1>&2
  23.         exit 1
  24.     fi
  25. fi
  26.  
  27. COMMAND="$1"
  28. shift
  29.  
  30.  
  31. if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  32.         ECHO=echo
  33. else
  34.         ECHO=:
  35. fi
  36.  
  37. $ECHO "Rather than invoking init scripts through /etc/init.d, use the service(8)"
  38. $ECHO "utility, e.g. service $INITSCRIPT $COMMAND"
  39.  
  40. # Only check if jobs are disabled if the currently _running_ version of
  41. # Upstart (which may be older than the latest _installed_ version)
  42. # supports such a query.
  43. #
  44. # This check is necessary to handle the scenario when upgrading from a
  45. # release without the 'show-config' command (introduced in
  46. # Upstart for Ubuntu version 0.9.7) since without this check, all
  47. # installed packages with associated Upstart jobs would be considered
  48. # disabled.
  49. #
  50. # Once Upstart can maintain state on re-exec, this change can be
  51. # dropped (since the currently running version of Upstart will always
  52. # match the latest installed version).
  53.  
  54. UPSTART_VERSION_RUNNING=$(initctl version|awk '{print $3}'|tr -d ')')
  55.  
  56. if dpkg --compare-versions "$UPSTART_VERSION_RUNNING" ge 0.9.7
  57. then
  58.     initctl show-config -e "$JOB"|grep -q '^  start on' || DISABLED=1
  59. fi
  60.  
  61. case $COMMAND in
  62. status)
  63.     $ECHO
  64.     $ECHO "Since the script you are attempting to invoke has been converted to an"
  65.     $ECHO "Upstart job, you may also use the $COMMAND(8) utility, e.g. $COMMAND $JOB"
  66.     $COMMAND "$JOB"
  67.     ;;
  68. start|stop)
  69.     $ECHO
  70.     $ECHO "Since the script you are attempting to invoke has been converted to an"
  71.     $ECHO "Upstart job, you may also use the $COMMAND(8) utility, e.g. $COMMAND $JOB"
  72.     if status "$JOB" 2>/dev/null | grep -q ' start/'; then
  73.         RUNNING=1
  74.     fi
  75.     if [ -z "$RUNNING" ] && [ "$COMMAND" = "stop" ]; then
  76.         exit 0
  77.     elif [ -n "$RUNNING" ] && [ "$COMMAND" = "start" ]; then
  78.         exit 0
  79.     elif [ -n "$DISABLED" ] && [ "$COMMAND" = "start" ]; then
  80.         exit 0
  81.     fi
  82.     $COMMAND "$JOB"
  83.     ;;
  84. restart)
  85.     $ECHO
  86.     $ECHO "Since the script you are attempting to invoke has been converted to an"
  87.     $ECHO "Upstart job, you may also use the stop(8) and then start(8) utilities,"
  88.     $ECHO "e.g. stop $JOB ; start $JOB. The restart(8) utility is also available."
  89.     if status "$JOB" 2>/dev/null | grep -q ' start/'; then
  90.         RUNNING=1
  91.     fi
  92.     if [ -n "$RUNNING" ] ; then
  93.         stop "$JOB"
  94.     fi
  95.     # If the job is disabled and is not currently running, the job is
  96.     # not restarted. However, if the job is disabled but has been forced into the
  97.     # running state, we *do* stop and restart it since this is expected behaviour
  98.     # for the admin who forced the start.
  99.     if [ -n "$DISABLED" ] && [ -z "$RUNNING" ]; then
  100.         exit 0
  101.     fi
  102.     start "$JOB"
  103.     ;;
  104. reload|force-reload)
  105.     $ECHO
  106.     $ECHO "Since the script you are attempting to invoke has been converted to an"
  107.     $ECHO "Upstart job, you may also use the reload(8) utility, e.g. reload $JOB"
  108.     reload "$JOB"
  109.     ;;
  110. *)
  111.     $ECHO
  112.     $ECHO "The script you are attempting to invoke has been converted to an Upstart" 1>&2
  113.     $ECHO "job, but $COMMAND is not supported for Upstart jobs." 1>&2
  114.     exit 1
  115. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement