Advertisement
danielhilst

generic init.d service

Oct 25th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # 2013 Daniel Hilst Selli <daniel.hilst@csi.ind.br>
  4. #
  5.  
  6. # Change this to start your service
  7. # also, set ARGS on /etc/default/<SERVICE>
  8. # as arguments to $PROG. Where SERVICE is the
  9. # name of this script at /etc/init.d
  10. PATH=/usr/bin:/bin:/sbin:/usr/sbin
  11. PROG=foo
  12.  
  13. # Guessing the service script name. It is /etc/rcN.d/S20service
  14. # while booting, and /etc/init.d/service if is interactively invocated
  15. # On both cases I want service. sed, perl, awk are slow :-)
  16. _nopath=${0##*/} # now we have S20service or service on $_nopath
  17. SERVICE=${_nopath#[SK][0-9][0-9]} # and now service
  18.  
  19. PIDFILE=${PIDFILE:-/var/run/$SERVICE.pid} # Overwrite this on /etc/default/$SERVICE
  20.  
  21. if test -r /etc/default/$SERVICE; then
  22. . /etc/default/$SERVICE
  23. fi
  24.  
  25. start()
  26. {
  27. start-stop-daemon --background --make-pidfile --pidfile $PIDFILE --exec $PROG -S -- $ARGS
  28. }
  29.  
  30. stop()
  31. {
  32. start-stop-daemon --pidfile $PIDFILE --exec $PROG -K
  33. }
  34.  
  35. status()
  36. {
  37. if kill -0 $(cat $PIDFILE) > /dev/null 2>&1; then
  38. return 0
  39. else
  40. return 1
  41. fi
  42. }
  43.  
  44. usage()
  45. {
  46. echo "$0 {start|stop|status|restart}"
  47. }
  48.  
  49. if test -z "$1"; then
  50. start
  51. exit 1
  52. fi
  53.  
  54. case $1 in
  55. start)
  56. echo -n "Starting $SERVICE ... "
  57. start
  58. sleep 1
  59. if status; then
  60. echo "DONE"
  61. else
  62. echo "FAIL"
  63. fi
  64. ;;
  65. stop)
  66. echo -n "Stopping $SERVICE ... "
  67. stop > /dev/null 2>&1
  68. sleep 1
  69. if status; then
  70. echo "FAIL"
  71. else
  72. echo "DONE"
  73. fi
  74. ;;
  75. restart)
  76. stop
  77. sleep 2
  78. start
  79. status
  80. ;;
  81. status)
  82. echo -n "$SERVICE is ... "
  83. if status; then
  84. echo "STARTED"
  85. else
  86. echo "STOPPED"
  87. fi
  88. ;;
  89. *)
  90. usage
  91. exit 1
  92. ;;
  93. esac
  94.  
  95. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement