1
0

service.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #
  2. # service: simple wrapper around start-stop-daemon
  3. #
  4. # Usage: service ACTION EXEC ARGS...
  5. #
  6. # Action:
  7. # -C check if EXEC is alive
  8. # -S start EXEC, passing it ARGS as its arguments
  9. # -K kill EXEC, sending it a TERM signal if not specified otherwise
  10. #
  11. # Environment variables exposed:
  12. # SERVICE_DAEMONIZE run EXEC in background
  13. # SERVICE_WRITE_PID create a pid-file and use it for matching
  14. # SERVICE_MATCH_EXEC use EXEC command-line for matching (default)
  15. # SERVICE_MATCH_NAME use EXEC process name for matching
  16. # SERVICE_USE_PID assume EXEC create its own pid-file and use it for matching
  17. # SERVICE_NAME process name to use (default to EXEC file part)
  18. # SERVICE_PID_FILE pid file to use (default to /var/run/$SERVICE_NAME.pid)
  19. # SERVICE_SIG signal to send when using -K
  20. # SERVICE_SIG_RELOAD default signal used when reloading
  21. # SERVICE_SIG_STOP default signal used when stopping
  22. # SERVICE_STOP_TIME time to wait for a process to stop gracefully before killing it
  23. # SERVICE_UID user EXEC should be run as
  24. # SERVICE_GID group EXEC should be run as
  25. #
  26. # SERVICE_DEBUG don't do anything, but show what would be done
  27. # SERVICE_QUIET don't print anything
  28. #
  29. SERVICE_QUIET=1
  30. SERVICE_SIG_RELOAD="HUP"
  31. SERVICE_SIG_STOP="TERM"
  32. SERVICE_STOP_TIME=5
  33. SERVICE_MATCH_EXEC=1
  34. service() {
  35. local ssd
  36. local exec
  37. local name
  38. local start
  39. ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}"
  40. case "$1" in
  41. -C)
  42. ssd="$ssd -K -t"
  43. ;;
  44. -S)
  45. ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}"
  46. start=1
  47. ;;
  48. -K)
  49. ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}"
  50. ;;
  51. *)
  52. echo "service: unknown ACTION '$1'" 1>&2
  53. return 1
  54. esac
  55. shift
  56. exec="$1"
  57. [ -n "$exec" ] || {
  58. echo "service: missing argument" 1>&2
  59. return 1
  60. }
  61. [ -x "$exec" ] || {
  62. echo "service: file '$exec' is not executable" 1>&2
  63. return 1
  64. }
  65. name="${SERVICE_NAME:-${exec##*/}}"
  66. [ -z "$SERVICE_USE_PID$SERVICE_WRITE_PID$SERVICE_PID_FILE" ] \
  67. || ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/$name.pid}"
  68. [ -z "$SERVICE_MATCH_NAME" ] || ssd="$ssd -n $name"
  69. ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}"
  70. [ -z "$SERVICE_MATCH_EXEC$start" ] || ssd="$ssd -x $exec"
  71. shift
  72. $ssd${1:+ -- "$@"}
  73. }
  74. service_check() {
  75. service -C "$@"
  76. }
  77. service_signal() {
  78. SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@"
  79. }
  80. service_start() {
  81. service -S "$@"
  82. }
  83. service_stop() {
  84. local try
  85. SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service -K "$@" || return 1
  86. while [ $((try++)) -lt $SERVICE_STOP_TIME ]; do
  87. service -C "$@" || return 0
  88. sleep 1
  89. done
  90. SERVICE_SIG="KILL" service -K "$@"
  91. sleep 1
  92. ! service -C "$@"
  93. }
  94. service_reload() {
  95. SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@"
  96. }