service - 在旧 linux 系统 (RHL6) 中为 Prometheus node exporter 创建一个 SysV 初始化脚本

我有一台服务器机器,它有 RHL6(Red Hat Linux 6)并且基于 SysV 初始化(没有 systemd 包),我想让我的 prometheus node exporter 从这台机器收集指标。

我在网上只能找到如何使用 systemctl (systemd) 创建节点导出器 service:基本上,您在 /etc/systemd/system 下创建一个 .service 文件,然后在其中编写如下内容:

[Unit]
Description=Node Exporter

After=network.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

然后你启动 service,在启动时启用它,等等使用这样的 systemctl 命令

sudo systemctl start node_exporter
sudo systemctl status node_exporter
sudo systemctl enable node_exporter

但问题是我没有安装 systemd 并且我无权更新服务器机器系统,所以我试图找到一种方法如何为节点导出器编写一个初始化脚本以放置在 / etc/rd.d/init.d 就我而言。

似乎 init.d 下的所有脚本都是 shell 脚本,声明了许多方法,如 start()、stop()、restart()、reload()、force_reload()、...

所以不像在systemd的基础上写service那么简单。

任何人都知道如何使用 SysV init 来做到这一点???

谢谢,

回答1

我设法为我的问题找到了解决方案。

脚本如下所示:

#!/bin/bash
#
# chkconfig: 2345 90 12
# description: node-exporter server
#

# Get function from functions library
. /etc/init.d/functions

# Start the service node-exporter
start() {
        echo -n "Starting node-exporter service: "
        /usr/sbin/node_exporter_service &
        ### Create the lock file ###
        touch /var/lock/subsys/node-exporter
        success $"node-exporter service startup"
        echo
}

# Restart the service node-exporter
stop() {
        echo -n "Shutting down node-exporter service: "
        killproc node_exporter_service
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/node-exporter
        echo
}

### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status node_exporter_service
        ;;
  restart|reload)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac

exit 0

我们将上述脚本放在名为“node-exporter”(不带 .sh)的 /etc/init.d 下,并将节点导出器的二进制文件放在 /usr/sbin 下(使用 systemd 我们将二进制文件放在 /usr/local/bin 下)。

您可以从此处 https://github.com/prometheus/node_exporter/releases 下载节点导出器的二进制文件。

然后我们使用命令 chkconfig --add node-exporter 将脚本文件添加到 services 列表中(检查它是否已经存在使用命令 chkconfig --list node-exporter)。

使用命令 chkconfig node-exporter on 启用 service。然后开始/停止/重新启动...我们使用命令/etc/init.d/node-exporter start/stop/restart ...

在启动脚本中,我们基本上运行二进制文件,在停止脚本中,我们通过名称终止进程。

我希望这会有用。

相似文章

随机推荐

最新文章