init.d script/service 对于 node_exporter

init.d script/service for node_exporter

我有 Gentoo 服务器,我在其中安装了 node_exporter。我无法在机器上创建 systemd 服务,因为 systemctl 不存在。现在我必须为 node_exporter 创建 init.d 服务。 我用下面的脚本创建了服务



#!/bin/sh
### BEGIN INIT INFO
# Provides: node_exporter
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description:
### END INIT INFO

 SCRIPT=<path to node_exporter.sh>
 RUNAS=root

  PIDFILE=/var/run/node_exporter.pid
  LOGFILE=/var/log/node_exporter.log

   start() {
       if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
           echo 'Service already running' >&2
           return 1
           fi
           echo 'Starting service' >&2
           local CMD="$SCRIPT &> \"$LOGFILE\" && echo $! > $PIDFILE"
           su -c "$CMD" $RUNAS > "$LOGFILE"
           echo 'Service started' >&2
   }

    stop() {
        if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
            echo 'Service not running' >&2
            return 1
            fi
            echo 'Stopping service' >&2
            kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
            echo 'Service stopped' >&2
    }

     uninstall() {
         echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
         local SURE
         read SURE
         if [ "$SURE" = "yes" ]; then
             stop
             rm -f "$PIDFILE"
             echo "Notice: log file is not be removed: '$LOGFILE'" >&2
             update-rc.d -f  remove
             rm -fv "[=13=]"
             fi
     }

      case "" in
      start)
      start
      ;;
      stop)
      stop
      ;;
      uninstall)
      uninstall
      ;;
      retart)
      stop
      start
      ;;
      *)
      echo "Usage: [=13=] {start|stop|restart|uninstall}"
      esac

Node_exporter.sh


#!/bin/sh

/opt/node_exporter/node_exporter --no-collector.diskstats

但这并没有完全启动该过程,它只停留在 Starting service。我在这里遗漏了什么吗

您可以尝试使用以下命令启动 node_exporter:

su - $RUNAS -c "$SCRIPT >> $LOGFILE 2>&1 &"
echo $! > $PIDFILE