init.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #! /usr/bin/env bash
  2. # chkconfig: 2345 80 05
  3. # description: Grafana web server & backend
  4. # processname: grafana
  5. # config: /etc/grafana/grafana.ini
  6. # pidfile: /var/run/grafana.pid
  7. ### BEGIN INIT INFO
  8. # Provides: grafana
  9. # Required-Start: $all
  10. # Required-Stop: $remote_fs $syslog
  11. # Default-Start: 2 3 4 5
  12. # Default-Stop: 0 1 6
  13. # Short-Description: Start grafana at boot time
  14. ### END INIT INFO
  15. # tested on
  16. # 1. New lsb that define start-stop-daemon
  17. # 3. Centos with initscripts package installed
  18. if [ -r /lib/lsb/init-functions ]; then
  19. source /lib/lsb/init-functions
  20. fi
  21. DAEMON_NAME="grafana"
  22. DAEMON_USER="grafana"
  23. DAEMON_PATH="/opt/grafana/current/grafana"
  24. DAEMON_OPTS="--config=/etc/grafana/grafana.ini web"
  25. DAEMON_PWD="/opt/grafana/current"
  26. DAEMON_PID="/var/run/${DAEMON_NAME}.pid"
  27. DAEMON_NICE=0
  28. DAEMON_LOG='/var/log/grafana/grafana.log'
  29. # If the daemon is not there, then exit.
  30. [ -x $DAEMON_PATH ] || exit 5
  31. if [ "x$STDOUT" == "x" ]; then
  32. STDOUT=/tmp/grafana.log
  33. fi
  34. function pidofproc() {
  35. if [ $# -ne 3 ]; then
  36. echo "Expected three arguments, e.g. $0 -p pidfile daemon-name"
  37. fi
  38. pid=`pgrep -f $3`
  39. local pidfile=`cat $2`
  40. if [ "x$pidfile" == "x" ]; then
  41. return 1
  42. fi
  43. if [ "x$pid" != "x" -a "$pidfile" == "$pid" ]; then
  44. return 0
  45. fi
  46. return 1
  47. }
  48. function killproc() {
  49. if [ $# -ne 3 ]; then
  50. echo "Expected three arguments, e.g. $0 -p pidfile signal"
  51. fi
  52. pid=`cat $2`
  53. kill -s $3 $pid
  54. }
  55. function log_failure_msg() {
  56. echo "$@" "[ FAILED ]"
  57. }
  58. function log_success_msg() {
  59. echo "$@" "[ OK ]"
  60. }
  61. do_start() {
  62. cd $DAEMON_PWD
  63. # Checked the PID file exists and check the actual status of process
  64. if [ -e $DAEMON_PID ]; then
  65. pidofproc -p $DAEMON_PID $DAEMON_PATH > /dev/null 2>&1 && status="0" || status="$?"
  66. # If the status is SUCCESS then don't need to start again.
  67. if [ "x$status" = "x0" ]; then
  68. log_failure_msg "$DAEMON_NAME process is running"
  69. exit 1 # Exit
  70. fi
  71. fi
  72. # Start the daemon.
  73. log_success_msg "Starting the process" "$DAEMON_NAME"
  74. # Start the daemon with the help of start-stop-daemon
  75. # Log the message appropriately
  76. if which start-stop-daemon > /dev/null 2>&1; then
  77. start-stop-daemon \
  78. --start --quiet --oknodo --background \
  79. --nicelevel $DAEMON_NICE \
  80. --chdir "${DAEMON_PWD}" \
  81. --pidfile "${DAEMON_PID}" --make-pidfile \
  82. --chuid "${DAEMON_USER}" \
  83. --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
  84. result=$?
  85. else
  86. touch ${DAEMON_PID}
  87. chown $DAEMON_USER "${DAEMON_PID}"
  88. #daemon --user $DAEMON_USER --pidfile $DAEMON_PID nohup $DAEMON_PATH $DAEMON_OPTS
  89. su -s /bin/sh -c "nohup ${DAEMON_PATH} --pidfile=${DAEMON_PID} ${DAEMON_OPTS} >> $STDOUT 3>&1 &" $DAEMON_USER
  90. fi
  91. log_success_msg "$DAEMON_NAME process was started"
  92. }
  93. do_stop() {
  94. local result
  95. pidofproc -p "${DAEMON_PID}" "${DAEMON_PATH}" > /dev/null
  96. if [ $? -ne 0 ]; then
  97. log_failure_msg "${DAEMON_NAME} is not started"
  98. result=0
  99. else
  100. log_success_msg "Stopping ${DAEMON_NAME}"
  101. killproc -p "${DAEMON_PID}" SIGTERM
  102. result=$?
  103. if [ $result = 0 ]; then
  104. log_success_msg "Stopped ${DAEMON_NAME}"
  105. rm "${DAEMON_PID}"
  106. fi
  107. fi
  108. return $result
  109. }
  110. do_restart() {
  111. local result
  112. do_stop
  113. result=$?
  114. sleep 2
  115. if [ $result = 0 ]; then
  116. do_start
  117. result=$?
  118. fi
  119. return $result
  120. }
  121. do_status() {
  122. if [ -e $DEAMON_PID ]; then
  123. pidofproc -p "${DAEMON_PID}" "${DAEMON_PATH}" > /dev/null
  124. if [ $? -ne 0 ]; then
  125. log_failure_msg "$DAEMON_NAME Process is not running"
  126. exit 1
  127. else
  128. log_success_msg "$DAEMON_NAME Process is running"
  129. exit 0
  130. fi
  131. else
  132. log_failure_msg "$DAEMON_NAME Process is not running"
  133. exit 3
  134. fi
  135. }
  136. do_usage() {
  137. echo $"Usage: $0 {start | stop | restart | status}"
  138. exit 1
  139. }
  140. case "$1" in
  141. start) do_start; exit $? ;;
  142. stop) do_stop; exit $? ;;
  143. restart) do_restart; exit $? ;;
  144. status) do_status; exit $? ;;
  145. *) do_usage; exit 1 ;;
  146. esac