Skip to content

Instantly share code, notes, and snippets.

@gpolaert
Last active December 14, 2017 21:50
Show Gist options
  • Select an option

  • Save gpolaert/2bc75731a368a74df6500ceda97ad228 to your computer and use it in GitHub Desktop.

Select an option

Save gpolaert/2bc75731a368a74df6500ceda97ad228 to your computer and use it in GitHub Desktop.
perfstats-to-syslog init.d script (lsb version)
#!/bin/sh
### BEGIN INIT INFO
# Provides: perfstats-to-syslog
# 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: perfstats-to-syslog is a daemon which collects system performance statistics periodically and stream it in clean JSON format to a syslog agent
### END INIT INFO
SCRIPT=/opt/perfstats-to-syslog/perfstats-to-syslog.py
RUNAS=root
PIDFILE=/var/run/perfstats-to-syslog.pid
LOGFILE=/var/log/perfstats-to-syslog.log
start() {
if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT &>> \"$LOGFILE\" & echo \$!"
su -c "$CMD" $RUNAS > "$PIDFILE"
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
echo 'Service stopped' >&2
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
esac
@mbrodala
Copy link

There is a typo here: retart instead of restart.

@gpolaert
Copy link
Author

Oups Oo 👍

@mbrodala
Copy link

This should be fixed:

-  if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
+  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE" ); then

This is useful to keep appending to the logfile instead of creating it anew on every service start:

-  local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
+  local CMD="$SCRIPT &>> \"$LOGFILE\" & echo \$!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment