我正在尝试在 centos 6.8 上为 prometheus 运行 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: Node exporter for prometheus written in Go
### END INIT INFO
DAEMON=/home/prometheus/node_exporter-0.14.0.linux-amd64
NAME=node_exporter
USER=prometheus
PIDFILE=/var/run/prometheus/$NAME.pid
LOGFILE=/var/log/prometheus/$NAME.log
ARGS=""
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
do_start_prepare()
{
mkdir -p `dirname $PIDFILE` || true
mkdir -p `dirname $LOGFILE` || true
chown -R $USER: `dirname $LOGFILE`
chown -R $USER: `dirname $PIDFILE`
}
do_start_cmd()
{
do_start_prepare
echo -n "Starting daemon: "$NAME
start-stop-daemon --chuid $USER -C --background --start --quiet --pidfile $PIDFILE --make-pidfile --exec $DAEMON -- $ARGS >> $LOGFILE 2>&1
echo "."
}
do_stop_cmd()
{
echo -n "Stopping daemon: "$NAME
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
rm $PIDFILE
echo "."
}
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 was not removed: '$LOGFILE'" >&2
update-rc.d -f <NAME> remove
rm -fv "$0"
fi
}
status() {
printf "%-50s" "Checking $NAME..."
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
printf "%s\n" "The process appears to be dead but pidfile still exists"
else
echo "Running, the PID is $PID"
fi
else
printf "%s\n" "Service not running"
fi
}
case "$1" in
start)
do_start_cmd
;;
stop)
do_stop_cmd
;;
status)
status
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
*)
echo "Usage: $1 {start|stop|status|restart|uninstall}"
exit 1
esac
exit 0
这是我运行它时的输出。
[prometheus@prometheus init.d]$ service node_exporter start
Starting daemon: node_exporter.
但当我检查状态时什么也没有发生:
[prometheus@prometheus init.d]$ service node_exporter status
Checking node_exporter... Service not running
这是 /var/log/prometheus/node_exporter.log 中的日志
/etc/init.d/node_exporter: line 35: start-stop-daemon: command not found
这是“which”的输出:
[prometheus@prometheus prometheus]$ which start-stop-daemon
/usr/bin/which: no start-stop-daemon in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
我确实需要一些指导,因为我主要使用 CentOS 7,并且不太熟悉 init.d 脚本
答案1
start-stop-daemon
是 Debian 专用脚本,仅存在于 Debian 及其衍生系统(如 Ubuntu)中。您不会在 CentOS 上找到它,事实上,它不应该被使用,因为它不在那里。
您可以直接启动和停止守护进程。
在以下情况下node_exporter
,该项目运送了CentOS 6 的初始化脚本您可以直接使用。您不需要自己编写。
答案2
对于 RHEL 6,我已经安装daemonize
并使用了此脚本:
https://github.com/utobi/prometheus-rpm/blob/master/node_exporter/contrib/node_exporter.init