前言
ubuntu 14.04的系统,因为是内网环境,特殊需求不允许连接外网,无法安装systemd相关命令,只能用service命令做启动。
• Prometheus 启动脚本
## sudo vim /etc/init.d/prometheus
代码如下(示例):
#!/bin/bash
### BEGIN INIT INFO
# Provides: prometheus
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts prometheus
# Description: starts the prometheus Process Manager daemon
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
prefix=/opt/prometheus/prometheus_2.36 # 安装的prometheus路径
exec_prefix=${prefix}
prometheus_BIN=${exec_prefix}/prometheus
prometheus_LOG=${exec_prefix}/prometheus.log
PID=$(ps -aux | grep "prometheus.yml" | grep -v grep | awk -F " " '{print $2}')
case "$1" in
start)
if [ -z "$PID" ]
then
# 原手动启动命令
/usr/bin/nohup $prometheus_BIN --config.file="${exec_prefix}/prometheus.yml" --web.listen-address="0.0.0.0:9090" --storage.tsdb.path="/opt/prometheus/prometheus_2.36/data" --storage.tsdb.retention=365d --web.enable-lifecycle > $prometheus_LOG 2>&1 &
else
echo -e "prometheus is running Pid:$PID \n"
exit 0
fi
echo -e "Starting prometheus $PID \n"
;;
stop)
if [ -z "$PID" ]
then
echo "not found prometheus: $PID"
else
kill -9 $PID
echo "stop prometheus.service"
fi
;;
reload)
if [ -z "$PID" ]
then
echo "not found prometheus: $PID"
else
kill -1 $PID
echo "reload prometheus:$PID"
fi
;;
status)
if [ -z "$PID" ]
then
echo "prometheus is stopped"
else
echo "prometheus is running"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|reload|status|restart}"
exit 1
;;
esac
• 赋执行权限
sudo chmod +x /etc/init.d/prometheus
• 验证结果