# description: Telegraf daemon
# Required-Stop: $remote_fs $syslog
# Short-Description: Start telegraf at boot time
# this init script supports three different variations:
# 1. New lsb that define start-stop-daemon
# 2. Old lsb that don't have start-stop-daemon but define, log, pidofproc and killproc
# 3. Centos installations without lsb-core installed
# In the third case we have to define our own functions which are very dumb
# and expect the args to be positioned correctly.
# Command-line options that can be set in /etc/default/telegraf. These will override
# any config file values.
if [ -r /lib/lsb/init-functions ]; then
source /lib/lsb/init-functions
DEFAULT=/etc/default/telegraf
if [ -z "$STDOUT" ]; then
if [ ! -f "$STDOUT" ]; then
mkdir -p `dirname $STDOUT`
if [ -z "$STDERR" ]; then
STDERR=/var/log/telegraf/telegraf.log
if [ ! -f "$STDERR" ]; then
mkdir -p `dirname $STDERR`
echo "Expected three arguments, e.g. $0 -p pidfile daemon-name"
if [ "x$pidfile" == "x" ]; then
if ps --pid "$pidfile" | grep -q $(basename $3); then
echo "Expected three arguments, e.g. $0 -p pidfile signal"
function log_failure_msg() {
function log_success_msg() {
# Process name ( For display )
# Daemon name, where is the actual executable
# pid file for the daemon
pidfile=/var/run/telegraf/telegraf.pid
piddir=`dirname $pidfile`
if [ ! -d "$piddir" ]; then
chown $USER:$GROUP $piddir
config=/etc/telegraf/telegraf.conf
confdir=/etc/telegraf/telegraf.d
# If the daemon is not there, then exit.
# Checked the PID file exists and check the actual status of process
if [ -e "$pidfile" ]; then
if pidofproc -p $pidfile $daemon > /dev/null; then
log_failure_msg "$name process is running"
log_failure_msg "$name pidfile has no corresponding process; ensure $name is stopped and remove $pidfile"
# Bump the file limits, before launching the daemon. These will carry over to
ulimit -n $OPEN_FILE_LIMIT
log_failure_msg "set open file limit to $OPEN_FILE_LIMIT"
log_success_msg "Starting the process" "$name"
if command -v startproc >/dev/null; then
startproc -u "$USER" -g "$GROUP" -p "$pidfile" -q -- "$daemon" -pidfile "$pidfile" -config "$config" -config-directory "$confdir" $TELEGRAF_OPTS
elif which start-stop-daemon > /dev/null 2>&1; then
start-stop-daemon --chuid $USER:$GROUP --start --quiet --pidfile $pidfile --exec $daemon -- -pidfile $pidfile -config $config -config-directory $confdir $TELEGRAF_OPTS >>$STDOUT 2>>$STDERR &
su -s /bin/sh -c "nohup $daemon -pidfile $pidfile -config $config -config-directory $confdir $TELEGRAF_OPTS >>$STDOUT 2>>$STDERR &" $USER
log_success_msg "$name process was started"
if pidofproc -p $pidfile $daemon > /dev/null; then
# periodically signal until process exists
if ! pidofproc -p $pidfile $daemon > /dev/null; then
killproc -p $pidfile SIGTERM 2>&1 >/dev/null
log_success_msg "$name process was stopped"
log_failure_msg "$name process is not running"
if pidofproc -p $pidfile $daemon > /dev/null; then
if killproc -p $pidfile SIGHUP; then
log_success_msg "$name process was reloaded"
log_failure_msg "$name failed to reload service"
log_failure_msg "$name process is not running"
$0 stop && sleep 2 && $0 start
# Check the status of the process.
if pidofproc -p $pidfile $daemon > /dev/null; then
log_success_msg "$name Process is running"
log_failure_msg "$name Process is not running"
log_failure_msg "$name Process is not running"
# For invalid arguments, print the usage message.
echo "Usage: $0 {start|stop|restart|status|version}"