github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/scripts/packages/init.sh (about)

     1  #!/bin/bash
     2  ### BEGIN INIT INFO
     3  # Provides:          pyroscope
     4  # Required-Start:    $local_fs $network $named $time $syslog
     5  # Required-Stop:     $local_fs $network $named $time $syslog
     6  # Default-Start:     2 3 4 5
     7  # Default-Stop:      0 1 6
     8  # Short-Description: Start the pyroscope service at boot time
     9  ### END INIT INFO
    10  
    11  # If you modify this, please make sure to also edit pyroscope.service
    12  
    13  # Script to execute when starting
    14  SCRIPT="/usr/bin/pyroscope"
    15  
    16  # Options to pass to the script on startup
    17  . /etc/default/pyroscope
    18  SCRIPT_OPTS="server"
    19  
    20  # User to run the process under
    21  RUNAS=pyroscope
    22  
    23  # PID file for process
    24  PIDFILE=/var/run/pyroscope-server.pid
    25  # Where to redirect logging to
    26  LOGFILE=/var/log/pyroscope/server.log
    27  
    28  start() {
    29      if [[ -f $PIDFILE ]]; then
    30          # PIDFILE exists
    31          if kill -0 $(cat $PIDFILE) &>/dev/null; then
    32              # PID up, service running
    33              echo '[OK] Service already running.' >&2
    34              return 0
    35          fi
    36      fi
    37      local CMD="$SCRIPT $SCRIPT_OPTS 1>>\"$LOGFILE\" 2>&1 & echo \$!"
    38      su -s /bin/sh -c "$CMD" $RUNAS > "$PIDFILE"
    39      if [[ -f $PIDFILE ]]; then
    40          # PIDFILE exists
    41          if kill -0 $(cat $PIDFILE) &>/dev/null; then
    42              # PID up, service running
    43              echo '[OK] Service successfully started.' >&2
    44              return 0
    45          fi
    46      fi
    47      echo '[ERROR] Could not start service.' >&2
    48      return 1
    49  }
    50  
    51  status() {
    52      if [[ -f $PIDFILE ]]; then
    53          # PIDFILE exists
    54          if ps -p $(cat $PIDFILE) &>/dev/null; then
    55              # PID up, service running
    56              echo '[OK] Service running.' >&2
    57              return 0
    58          fi
    59      fi
    60      echo '[ERROR] Service not running.' >&2
    61      return 1
    62  }
    63  
    64  stop() {
    65      if [[ -f $PIDFILE ]]; then
    66          # PIDFILE still exists
    67          if kill -0 $(cat $PIDFILE) &>/dev/null; then
    68              # PID still up
    69              kill -15 $(cat $PIDFILE) &>/dev/null && rm -f "$PIDFILE" &>/dev/null
    70              if [[ "$?" = "0" ]]; then
    71                  # Successful stop
    72                  echo '[OK] Service stopped.' >&2
    73                  return 0
    74              else
    75                  # Unsuccessful stop
    76                  echo '[ERROR] Could not stop service.' >&2
    77                  return 1
    78              fi
    79          fi
    80      fi
    81      echo "[OK] Service already stopped."
    82      return 0
    83  }
    84  
    85  case "$1" in
    86      start)
    87          if [[ "$UID" != "0" ]]; then
    88              echo "[ERROR] Permission denied."
    89              exit 1
    90          fi
    91          start
    92          ;;
    93      status)
    94          status
    95          ;;
    96      stop)
    97          if [[ "$UID" != "0" ]]; then
    98              echo "[ERROR] Permission denied."
    99              exit 1
   100          fi
   101          stop
   102          ;;
   103      restart)
   104          stop
   105          start
   106          ;;
   107      *)
   108          echo "Usage: $0 {start|status|stop|restart}"
   109          esac