Advertisement
Guest User

Influxdb init.sh

a guest
Feb 25th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.79 KB | None | 0 0
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides:          influxd
  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 the InfluxDB process
  9. ### END INIT INFO
  10.  
  11. # If you modify this, please make sure to also edit influxdb.service
  12.  
  13. # Command-line options that can be set in /etc/default/influxdb.  These will override
  14. # any config file values.
  15. DEFAULT=/etc/default/influxdb
  16.  
  17. # Daemon options
  18. INFLUXD_OPTS=
  19.  
  20. # Process name ( For display )
  21. NAME=influxdb
  22.  
  23. # User and group
  24. USER=influxdb
  25. GROUP=influxdb
  26.  
  27. # Check for sudo or root privileges before continuing
  28. if [ "$UID" != "0" ]; then
  29.     echo "You must be root to run this script"
  30.     exit 1
  31. fi
  32.  
  33. # Daemon name, where is the actual executable If the daemon is not
  34. # there, then exit.
  35. DAEMON=/usr/bin/influxd
  36. if [ ! -x $DAEMON ]; then
  37.     echo "Executable $DAEMON does not exist!"
  38.     exit 5
  39. fi
  40.  
  41. # Configuration file
  42. CONFIG=/etc/influxdb/influxdb.conf
  43.  
  44. # PID file for the daemon
  45. PIDFILE=/var/run/influxdb/influxd.pid
  46. PIDDIR=`dirname $PIDFILE`
  47. if [ ! -d "$PIDDIR" ]; then
  48.     mkdir -p $PIDDIR
  49.     chown $USER:$GROUP $PIDDIR
  50. fi
  51.  
  52. # Max open files
  53. OPEN_FILE_LIMIT=65536
  54.  
  55. if [ -r /lib/lsb/init-functions ]; then
  56.     source /lib/lsb/init-functions
  57. fi
  58.  
  59. # Logging
  60. if [ -z "$STDOUT" ]; then
  61.     STDOUT=/dev/null
  62. fi
  63.  
  64. if [ ! -f "$STDOUT" ]; then
  65.     mkdir -p $(dirname $STDOUT)
  66. fi
  67.  
  68. if [ -z "$STDERR" ]; then
  69.     STDERR=/var/log/influxdb/influxd.log
  70. fi
  71.  
  72. if [ ! -f "$STDERR" ]; then
  73.     mkdir -p $(dirname $STDERR)
  74. fi
  75.  
  76. # Override init script variables with DEFAULT values
  77. if [ -r $DEFAULT ]; then
  78.     source $DEFAULT
  79. fi
  80.  
  81. function log_failure_msg() {
  82.     echo "$@" "[ FAILED ]"
  83. }
  84.  
  85. function log_success_msg() {
  86.     echo "$@" "[ OK ]"
  87. }
  88.  
  89. function start() {
  90.     # Check if config file exist
  91.     if [ ! -r $CONFIG ]; then
  92.         log_failure_msg "config file $CONFIG doesn't exist (or you don't have permission to view)"
  93.         exit 4
  94.     fi
  95.  
  96.     # Check that the PID file exists, and check the actual status of process
  97.     if [ -f $PIDFILE ]; then
  98.         PID="$(cat $PIDFILE)"
  99.         if kill -0 "$PID" &>/dev/null; then
  100.             # Process is already up
  101.             log_success_msg "$NAME process is already running"
  102.             return 0
  103.         fi
  104.     else
  105.         su -s /bin/sh -c "touch $PIDFILE" $USER &>/dev/null
  106.         if [ $? -ne 0 ]; then
  107.             log_failure_msg "$PIDFILE not writable, check permissions"
  108.             exit 5
  109.         fi
  110.     fi
  111.  
  112.     # Bump the file limits, before launching the daemon. These will
  113.     # carry over to launched processes.
  114.     ulimit -n $OPEN_FILE_LIMIT
  115.     if [ $? -ne 0 ]; then
  116.         log_failure_msg "Unable to set ulimit to $OPEN_FILE_LIMIT"
  117.         exit 1
  118.     fi
  119.  
  120.     # Launch process
  121.     echo "Starting $NAME..."
  122.     if command -v start-stop-daemon &>/dev/null; then
  123.         start-stop-daemon \
  124.             --chuid $USER:$GROUP \
  125.             --start \
  126.             --quiet \
  127.             --pidfile $PIDFILE \
  128.             --exec $DAEMON \
  129.             -- \
  130.             -pidfile $PIDFILE \
  131.             -config $CONFIG \
  132.             $INFLUXD_OPTS >>$STDOUT 2>>$STDERR &
  133.     else
  134.         local CMD="$DAEMON -pidfile $PIDFILE -config $CONFIG $INFLUXD_OPTS >>$STDOUT 2>>$STDERR &"
  135.         su -s /bin/sh -c "$CMD" $USER
  136.     fi
  137.  
  138.     # Sleep to verify process is still up
  139.     sleep 1
  140.     if [ -f $PIDFILE ]; then
  141.         # PIDFILE exists
  142.         if kill -0 $(cat $PIDFILE) &>/dev/null; then
  143.             # PID up, service running
  144.             log_success_msg "$NAME process was started"
  145.             return 0
  146.         fi
  147.     fi
  148.     log_failure_msg "$NAME process was unable to start"
  149.     exit 1
  150. }
  151.  
  152. function stop() {
  153.     # Stop the daemon.
  154.     if [ -f $PIDFILE ]; then
  155.         local PID="$(cat $PIDFILE)"
  156.         if kill -0 $PID &>/dev/null; then
  157.             echo "Stopping $NAME..."
  158.             # Process still up, send SIGTERM and remove PIDFILE
  159.             kill -s TERM $PID &>/dev/null && rm -f "$PIDFILE" &>/dev/null
  160.             n=0
  161.             while true; do
  162.                 # Enter loop to ensure process is stopped
  163.                 kill -0 $PID &>/dev/null
  164.                 if [ "$?" != "0" ]; then
  165.                     # Process stopped, break from loop
  166.                     log_success_msg "$NAME process was stopped"
  167.                     return 0
  168.                 fi
  169.  
  170.                 # Process still up after signal, sleep and wait
  171.                 sleep 1
  172.                 n=$(expr $n + 1)
  173.                 if [ $n -eq 30 ]; then
  174.                     # After 30 seconds, send SIGKILL
  175.                     echo "Timeout exceeded, sending SIGKILL..."
  176.                     kill -s KILL $PID &>/dev/null
  177.                 elif [ $? -eq 40 ]; then
  178.                     # After 40 seconds, error out
  179.                     log_failure_msg "could not stop $NAME process"
  180.                     exit 1
  181.                 fi
  182.             done
  183.         fi
  184.     fi
  185.     log_success_msg "$NAME process already stopped"
  186. }
  187.  
  188. function restart() {
  189.     # Restart the daemon.
  190.     stop
  191.     start
  192. }
  193.  
  194. function status() {
  195.     # Check the status of the process.
  196.     if [ -f $PIDFILE ]; then
  197.         PID="$(cat $PIDFILE)"
  198.         if kill -0 $PID &>/dev/null; then
  199.             log_success_msg "$NAME process is running"
  200.             exit 0
  201.         fi
  202.     fi
  203.     log_failure_msg "$NAME process is not running"
  204.     exit 1
  205. }
  206.  
  207. case $1 in
  208.     start)
  209.         start
  210.         ;;
  211.  
  212.     stop)
  213.         stop
  214.         ;;
  215.  
  216.     restart)
  217.         restart
  218.         ;;
  219.  
  220.     status)
  221.         status
  222.         ;;
  223.  
  224.     version)
  225.         $DAEMON version
  226.         ;;
  227.  
  228.     *)
  229.         # For invalid arguments, print the usage message.
  230.         echo "Usage: $0 {start|stop|restart|status|version}"
  231.         exit 2
  232.         ;;
  233. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement