github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/bin/package/installation/initd.sh (about)

     1  #!/bin/bash
     2  ### BEGIN INIT INFO
     3  # Provides:          mysterium-node
     4  # Required-Start:    $all
     5  # Default-Start:     2 3 4 5
     6  # Default-Stop:      0 1 6
     7  # Short-Description: Start the Mysterium VPN node process
     8  ### END INIT INFO
     9  
    10  # If you modify this, please make sure to also edit mysterium-node.service
    11  
    12  OS_DIR_BIN="/usr/bin"
    13  OS_DIR_CONFIG="/etc/mysterium-node"
    14  OS_DIR_DATA="/var/lib/mysterium-node"
    15  OS_DIR_LOG="/var/log/mysterium-node"
    16  OS_DIR_RUN="/var/run/mysterium-node"
    17  
    18  # Process name (For display)
    19  DAEMON_NAME="mysterium-node"
    20  #Daemon name, where is the actual executable
    21  DAEMON_BIN="$OS_DIR_BIN/myst"
    22  # User and group
    23  DAEMON_USER="mysterium-node"
    24  DAEMON_GROUP="mysterium-node"
    25  # PID file for the daemon
    26  DAEMON_PIDFILE="$OS_DIR_RUN/daemon.pid"
    27  # Logging
    28  DAEMON_STDOUT="$OS_DIR_LOG/daemon.log"
    29  DAEMON_STDERR="$OS_DIR_LOG/error.log"
    30  # Command-line options that can be set in /etc/default/mysterium-node.
    31  # These will override any config file values.
    32  DAEMON_DEFAULT="/etc/default/mysterium-node"
    33  
    34  # Check for sudo or root privileges before continuing
    35  if [ "$UID" != "0" ]; then
    36      echo "You must be root to run this script"
    37      exit 1
    38  fi
    39  
    40  # If the daemon is not there, then exit.
    41  if [ ! -x $DAEMON_BIN ]; then
    42      echo "Executable $DAEMON_BIN does not exist!"
    43      exit 5
    44  fi
    45  
    46  # Create directory for pid file
    47  PIDDIR=`dirname $DAEMON_PIDFILE`
    48  if [ ! -d "$PIDDIR" ]; then
    49      mkdir -p $PIDDIR
    50      chown $DAEMON_USER:$DAEMON_GROUP $PIDDIR
    51  fi
    52  
    53  
    54  # Create directory for logs
    55  LOGDIR=`dirname $DAEMON_STDOUT`
    56  if [ ! -d "$LOGDIR" ]; then
    57      mkdir -p $LOGDIR
    58      chown -R -L $DAEMON_USER:$DAEMON_GROUP $LOGDIR
    59  fi
    60  LOGDIR=`dirname $DAEMON_STDERR`
    61  if [ ! -d "$LOGDIR" ]; then
    62      mkdir -p $LOGDIR
    63      chown -R -L $DAEMON_USER:$DAEMON_GROUP $LOGDIR
    64  fi
    65  
    66  if [ -r /lib/lsb/init-functions ]; then
    67      source /lib/lsb/init-functions
    68  fi
    69  
    70  # Override init script variables with DEFAULT values
    71  if [ -r $DAEMON_DEFAULT ]; then
    72      source $DAEMON_DEFAULT
    73  fi
    74  
    75  function log_failure_msg() {
    76      echo "$@" "[ FAILED ]"
    77  }
    78  
    79  function log_success_msg() {
    80      echo "$@" "[ OK ]"
    81  }
    82  
    83  function start() {
    84      # Check that the PID file exists, and check the actual status of process
    85      if [ -f $DAEMON_PIDFILE ]; then
    86          PID="$(cat $DAEMON_PIDFILE)"
    87          if kill -0 "$PID" &>/dev/null; then
    88              # Process is already up
    89              log_success_msg "$DAEMON_NAME process is already running"
    90              return 0
    91          fi
    92      else
    93          su -s /bin/sh -c "touch $DAEMON_PIDFILE" $DAEMON_USER &>/dev/null
    94          if [ $? -ne 0 ]; then
    95              log_failure_msg "$DAEMON_PIDFILE not writable, check permissions"
    96              exit 5
    97          fi
    98      fi
    99  
   100      # Launch process
   101      echo "Starting $DAEMON_NAME..."
   102      start-stop-daemon \
   103          --start \
   104          --quiet \
   105          --background \
   106          --no-close \
   107          --make-pidfile \
   108          --pidfile $DAEMON_PIDFILE \
   109          --user $DAEMON_USER \
   110          --group $DAEMON_GROUP \
   111          --exec $DAEMON_BIN \
   112          -- \
   113          --config-dir=$OS_DIR_CONFIG \
   114          --script-dir=$OS_DIR_CONFIG \
   115          --data-dir=$OS_DIR_DATA \
   116          --runtime-dir=$OS_DIR_RUN \
   117          $DAEMON_OPTS \
   118          service \
   119          --agreed-terms-and-conditions \
   120          $SERVICE_OPTS \
   121          >>$DAEMON_STDOUT \
   122          2>>$DAEMON_STDERR
   123  
   124      # Sleep to verify process is still up
   125      sleep 1
   126      if [ -f $DAEMON_PIDFILE ]; then
   127          # PIDFILE exists
   128          if kill -0 $(cat $DAEMON_PIDFILE) &>/dev/null; then
   129              # PID up, service running
   130              log_success_msg "$DAEMON_NAME process was started"
   131              return 0
   132          fi
   133      fi
   134      log_failure_msg "$DAEMON_NAME process was unable to start"
   135      exit 1
   136  }
   137  
   138  function stop() {
   139      # Stop the daemon.
   140      if [ -f $DAEMON_PIDFILE ]; then
   141          local PID="$(cat $DAEMON_PIDFILE)"
   142          if kill -0 $PID &>/dev/null; then
   143              echo "Stopping $DAEMON_NAME..."
   144              # Process still up, send SIGTERM and remove PIDFILE
   145              kill -s TERM $PID &>/dev/null && rm -f "$DAEMON_PIDFILE" &>/dev/null
   146              n=0
   147              while true; do
   148                  # Enter loop to ensure process is stopped
   149                  kill -0 $PID &>/dev/null
   150                  if [ "$?" != "0" ]; then
   151                      # Process stopped, break from loop
   152                      log_success_msg "$DAEMON_NAME process was stopped"
   153  
   154                      # Kill open Openvpn, until signal handling will be implemented
   155                      killall openvpn
   156  
   157                      return 0
   158                  fi
   159  
   160                  # Process still up after signal, sleep and wait
   161                  sleep 1
   162                  n=$(expr $n + 1)
   163                  if [ $n -eq 30 ]; then
   164                      # After 30 seconds, send SIGKILL
   165                      echo "Timeout exceeded, sending SIGKILL..."
   166                      kill -s KILL $PID &>/dev/null
   167  
   168                      # Kill open Openvpn, until signal handling will be implemented
   169                      killall openvpn
   170                  elif [ $? -eq 40 ]; then
   171                      # After 40 seconds, error out
   172                      log_failure_msg "could not stop $DAEMON_NAME process"
   173                      exit 1
   174                  fi
   175              done
   176          fi
   177      fi
   178      log_success_msg "$DAEMON_NAME process already stopped"
   179  }
   180  
   181  function restart() {
   182      # Restart the daemon.
   183      stop
   184      start
   185  }
   186  
   187  function status() {
   188      # Check the status of the process.
   189      if [ -f $DAEMON_PIDFILE ]; then
   190          PID="$(cat $DAEMON_PIDFILE)"
   191          if kill -0 $PID &>/dev/null; then
   192              log_success_msg "$DAEMON_NAME process is running"
   193              exit 0
   194          fi
   195      fi
   196      log_failure_msg "$DAEMON_NAME process is not running"
   197      exit 1
   198  }
   199  
   200  case $1 in
   201      start)
   202          start
   203          ;;
   204  
   205      stop)
   206          stop
   207          ;;
   208  
   209      restart)
   210          restart
   211          ;;
   212  
   213      status)
   214          status
   215          ;;
   216  
   217      *)
   218          # For invalid arguments, print the usage message.
   219          echo "Usage: $0 {start|stop|restart|status}"
   220          exit 2
   221          ;;
   222  esac