init.sh 3.8 KB

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