Skip to content

Instantly share code, notes, and snippets.

@ezarowny
Created May 15, 2016 17:33
Show Gist options
  • Save ezarowny/25edb2084b6dedf9171c08fb36017d0c to your computer and use it in GitHub Desktop.
Save ezarowny/25edb2084b6dedf9171c08fb36017d0c to your computer and use it in GitHub Desktop.
flexget linux service
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment