Skip to content

Instantly share code, notes, and snippets.

@ipcpu
Created May 28, 2020 02:10
Show Gist options
  • Save ipcpu/3c1368cdc0fa2fddb4d541bba851dd19 to your computer and use it in GitHub Desktop.
Save ipcpu/3c1368cdc0fa2fddb4d541bba851dd19 to your computer and use it in GitHub Desktop.
node_exporter.init.d on centos 6
#!/bin/sh
# chkconfig: 2345 60 20
# description: node_exporter
# Source function library.
. /etc/init.d/functions
NAME=node_exporter
DFLTRUNAS=root
SCRIPT="/data/apps/exporters/bin/${NAME}"
PIDFILE="/var/run/${NAME}.pid"
LOGFILE="/var/log/${NAME}.log"
EXPORTER_ARGS="--collector.processes --collector.textfile.directory /data/apps/exporters/collectorfile"
start() {
if [ -f "${PIDFILE}" ] && kill -0 $(cat "${PIDFILE}") &> /dev/null; then
echo "${NAME} already running with PID $(cat ${PIDFILE})" >&2
return 1
fi
echo "Starting ${NAME}" >&2
if [ -z $RUNAS ]; then
RUNAS=${DFLTRUNAS}
fi
daemon --user $RUNAS --pidfile="$PIDFILE" "${SCRIPT} ${EXPORTER_ARGS} &" 2&> $LOGFILE
echo `pidof $NAME` > ${PIDFILE}
echo "${NAME} started with PID $(cat ${PIDFILE})" >&2
sleep 1
if [ -f "${PIDFILE}" ] && kill -0 $(cat "${PIDFILE}") &> /dev/null; then
echo "${NAME} started successfully." >&2
else
echo "${NAME} was not started OK"
return 1
fi
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE") &> /dev/null; then
echo "${NAME} not running" >&2
return 1
fi
echo "Stopping ${NAME}..." >&2
kill -15 $(cat "$PIDFILE")
rm -f "$PIDFILE"
echo "${NAME} stopped" >&2
}
status() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE") &> /dev/null; then
echo "${NAME} is not running" >&2
return 3
else
echo "${NAME} is running" >&2
return 0
fi
}
uninstall() {
echo -n "Are you really sure you want to uninstall ${NAME}? 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 <NAME> remove
rm -fv "$0"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
uninstall)
uninstall
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status|uninstall}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment