Instantly share code, notes, and snippets.

Embed
What would you like to do?
Netdata init script for Synology DSM
#!/bin/bash
# Install this as `/etc/rc.netdata` and call it from `/etc/rc.local`
# to have it executed on boot.
NETDATA_DIR=/opt/netdata
DAEMON=$NETDATA_DIR/bin/srv/netdata
TIMEOUT=30 # seconds to wait for netdata to exit
[ -x $DAEMON ] || exit 0
function get_pid() {
pid=$( ps ax | grep $DAEMON | grep -v grep | awk '{ print $1 }' )
}
function check_netdata() {
count=1
while [ $count -lt $TIMEOUT ]; do
get_pid
if [ -z $pid ]; then
break
else
sleep 1
count=$(( $count + 1))
fi
done
if [ $count -eq $TIMEOUT ]; then
echo "Timeout exceeded waiting for netdata to exit."
echo "Use '$0 kill' to force netdata to exit."
exit 1
fi
}
function stop_netdata() {
get_pid
if [ "x$pid" != "x" ]; then
if [ "x$1" = "xforce" ]; then
kill -9 $pid
else
kill $pid
fi
else
echo "No running netdata found."
fi
}
function start_netdata() {
get_pid
if [ "x$pid" = "x" ]; then
echo "Starting Netdata."
$DAEMON
else
echo "Netdata already running with pid $pid"
fi
}
case "$1" in
start)
start_netdata
;;
stop)
echo "Shutting down Netdata."
stop_netdata
;;
restart)
echo "Shutting down Netdata."
stop_netdata
if [ "x$pid" != "x" ]; then
echo "Waiting maximum of $TIMEOUT seconds for netdata to exit."
fi
check_netdata
start_netdata
;;
kill)
echo "Killing netdata."
stop_netdata force
;;
status)
get_pid
if [ "x$pid" != "x" ]; then
echo "Netdata running with PID $pid"
else
echo "No running netdata found."
fi
;;
*)
echo "Usage: $0 {start|stop|kill|restart|status}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment