Flexget init script

I've been using Flexget for the past two years or so as a download automator.

Since I wrote an init script for it a while back, and it is compatible with Debian Jessie / systemd, I figured I'd share it here.

The script

All of the following should be done as the root user.

First, create a /etc/default/flexget file with the following content :

# Configuration file for /etc/init.d/flexget

# User to run flexget as.
# Daemon will not start if left empty.
FGUSER=""

# Full path to the flexget config.yml file to use.
# Defaults to FGUSER $HOME/.flexget/config.yml
CONFIG=""

# Path to the directory where flexget should log. Do not add trailing slash.
# Defaults to the FGUSER $HOME/.flexget directory
LOG=""

# Log verbosity 
# Available options : none critical error warning info verbose debug trace
# Defaults to info
LEVEL=""

Please note that the FGUSER variable needs to be defined for the daemon to start. It can be set to your current user, or you can run flexget as its own user.

You can create a flexget user with the following command :

useradd -m -d /var/lib/flexget -r -s /bin/false flexget

Then, create the /etc/init.d/flexget file :

#!/bin/bash

### BEGIN INIT INFO
# Provides:          flexget
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Flexget
# Description:       FlexGet is a multipurpose automation tool 
#                    for content like torrents, nzbs, podcasts,
#                    comics, series, movies, etc.
### END INIT INFO

# Author: Antoine Joubert, 19/01/2014

NAME="flexget"
DAEMON="/usr/local/bin/flexget"
SETTINGS="/etc/default/$NAME"

DESC="Flexget"
PIDFILE="/var/run/$NAME.pid"

set -e

. /lib/lsb/init-functions

unset FGUSER CONFIG LOG LEVEL

# Exit if flexget not installed
if [ ! -x "$DAEMON" ]; then
  log_action_msg "$DESC: Could not find flexget executable. Exiting."
  exit 2
fi

# Read configuration variables
if [ -r /etc/default/$NAME ]; then
  . /etc/default/$NAME
else
  log_action_msg "$DESC: /etc/default/$NAME not found. Exiting."
  exit 2
fi

# Exit if FGUSER has not been set in /etc/default/flexget
if [ -z $FGUSER ]; then
  log_action_msg "$DESC: FGUSER not set in /etc/default/$NAME. Exiting."
  exit 2
fi

# Function to verify if flexget is already running
run_check() {
  if [ -e $PIDFILE ]; then
    status_of_proc -p $PIDFILE $DAEMON $NAME > /dev/null && RETVAL=0 || RETVAL="$?"
  else
    RETVAL="2"
  fi
}

end_log() {
  if [ $RETVAL -eq 0 ]; then
    log_end_msg 0
    return 0
  else
    log_end_msg 1
    exit 1
  fi
}

# Function to define config file, log file and log level
conf_check() {
  if [ -z $CONFIG ]; then
    OPTIONS="$OPTIONS"
  else
    OPTIONS="-c $CONFIG"
  fi

  if [ -z $LOG ]; then
    OPTIONS="$OPTIONS"
  else
    OPTIONS="$OPTIONS -l $LOG/flexget.log"
    if [ ! -d $LOG ]; then 
      mkdir -p -m 750 $LOG
      chown $FGUSER $LOG
    fi
  fi

  if [ -z $LEVEL ]; then
    OPTIONS="$OPTIONS"
  else
    OPTIONS="$OPTIONS -L $LEVEL"
  fi
}

start_flexget() {
  run_check
  if [ $RETVAL = 0 ]; then
    log_action_msg "$DESC: Already running with PID $(cat $PIDFILE). Aborting."
    exit 2
  else
    conf_check
    log_daemon_msg "$DESC: Starting the daemon."
    start-stop-daemon --start --background --quiet --pidfile $PIDFILE --make-pidfile \
    --chuid $FGUSER --user $FGUSER --exec $DAEMON -- $OPTIONS daemon start
    RETVAL=$?
    end_log
  fi
}

stop_flexget() {
  run_check
  if [ $RETVAL = 0 ]; then
    log_daemon_msg "$DESC: Stopping the daemon."
    start-stop-daemon --stop --quiet --chuid "$FGUSER" --pidfile "$PIDFILE" --retry 30
    RETVAL=$?
    [ -e "$PIDFILE" ] && rm -f "$PIDFILE"
    end_log
  else
    log_action_msg "$DESC: Not currently running. Aborting."
  exit 2
  fi
}

status_flexget() {
  run_check
  if [ $RETVAL = 0 ]; then
    log_action_msg "$DESC: Currently running with PID $(cat $PIDFILE)."
  else
    log_action_msg "$DESC: Not currently running."
  fi
  exit $RETVAL
}

case "$1" in
  start)
    start_flexget
  ;;
  stop)
    stop_flexget
  ;;
  restart)
    stop_flexget && sleep 2 && start_flexget
  ;;
  status)
    status_flexget
  ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
  ;;
esac

exit 0

Then, give execution rights to the script :

chmod +x /etc/init.d/flexget

And then, generate the necessary symlinks for the service to start on boot :

Debian Jessie

systemctl enable flexget

Debian Wheezy

insserv flexget

To start, stop or check if the daemon is running :

Debian Jessie

systemctl start flexget
systemctl stop flexget
systemctl status flexget

Debian Wheezy / Jessie

service flexget start
service flexget stop
service flexget status

Debian Wheezy

/etc/init.d/flexget start
/etc/init.d/flexget stop
/etc/init.d/flexget status

Conclusion

That's all ! If you are using this script, please let me know in the comment section below !