0

我有 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 "$0"
             fi
     }

      case "$1" in
      start)
      start
      ;;
      stop)
      stop
      ;;
      uninstall)
      uninstall
      ;;
      retart)
      stop
      start
      ;;
      *)
      echo "Usage: $0 {start|stop|restart|uninstall}"
      esac

Node_exporter.sh


#!/bin/sh

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

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

4

1 回答 1

0

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

su - $RUNAS -c "$SCRIPT >> $LOGFILE 2>&1 &"
echo $! > $PIDFILE
于 2019-11-07T15:16:08.497 回答