Newznab Updates as a Service (Ubuntu)

By Johnny Rollerfeet's Echo Chamber - Last updated: Wednesday, December 2, 2015 - Save & Share - Leave a Comment

Disclaimer: I am just a hobbyist, so don’t be surprised if I am unable to answer any questions you have. That said, feel free to ask and I’ll do my best. I am on Stack Overflow, so I would encourage you to ask questions there then leave a link here. That way others could benefit from your questions.

I’m running newznab on Ubuntu 14.04.3 LTS with MySOL 5.6.27, Apache 2.4.7, and PHP 5.5.9-1ubuntu4.14 including SABnzbd with EasyNews.com.

I was using the update script /var/www/newznab/misc/update_scripts/nix_scripts/newznab_screen.sh in a screen session, as recommended by newznab install/configuration sites. it froze all the time and required a lot of management to keep it running, including having to rerun it after every reboot. So, I have been working on creating a solution that runs as a service which allows it to run after reboot, and restarting it from the command line is simpler.

Another goal was to create a process that could be monitored, and could generate an email alert if the script hung. I completed the monitoring and email alerts using Nagios 4.1.1, but I am still working on an automated restart (which should be possible with Nagios.)

My first step was creating a file the script could touch before every cycle. There are probably files that could serve this purpose within the standard newznab configuration, but I struggled to find one that was easy to identify. I created this:

user@server:/$ touch /var/www/newznab/misc/monitor.txt
user@server:/$ vi /var/www/newznab/misc/monitor.txt
  > This file will be “touch"ed to show updates are happening.
user@server:/$

Next I edited this script:

user@server:/$ more /var/www/newznab/misc/update_scripts/nix_scripts/newznab_ubuntu.sh
#!/bin/bash
#
# Ian – 16/11/2011
# /etc/init.d/newznab: start and stop the newznab update script
#
# run update-rc.d newznab_ubuntu.sh defaults
### BEGIN INIT INFO
# Provides:          Newznab
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start newznab at boot time
# Description:       Enable newznab service provided by daemon.
### END INIT INFO
# Newznab variables
NN_PATH=”/var/www/newznab/misc/update_scripts"
NN_BINUP=“update_binaries.php”
NN_RELUP=“update_releases.php”
NN_OPTIMISE=“optimise_db.php”
NN_UPDATETV=“update_tvschedule.php”
NN_UPDATETH=“update_theaters.php”
NN_SLEEP_TIME=“60” # in seconds . 10sec is good for 100s of groups. 600sec might be a good start for fewer.
NN_PID_PATH=“/var/run/”
NN_LOGFILE=“/var/log/newznab_ubuntu.log”
PIDFILE=“newznab_binup.pid”
LOOPCOUNTER=0
test -f /lib/lsb/init-functions || exit 1
. /lib/lsb/init-functions
case “$1” in
start)
[ -f $NN_PID_PATH$PIDFILE ] && { echo -e “$0 is already ruNNing.\n”; false; }
      echo “Starting Newznab binaries update” >> $NN_LOGFILE 2>&1
      cd $NN_PATH
(
set -e
LASTOPTIMIZE=`date +%s`
echo “Last optimize is $LASTOPTIMIZE” >> $NN_LOGFILE 2>&1
      while :
do
echo “Touching ${NN_PATH}/../monitor.txt” >> $NN_LOGFILE 2>&1
touch ${NN_PATH}/../monitor.txt
LOOPCOUNTER=$((LOOPCOUNTER+1))
cd $NN_PATH
echo “Updating binaries” >> $NN_LOGFILE 2>&1
php $NN_BINUP >> $NN_LOGFILE 2>&1
echo “Updating releases” >> $NN_LOGFILE 2>&1
php $NN_RELUP >> $NN_LOGFILE 2>&1
echo “Sleeping for $NN_SLEEP_TIME” >> $NN_LOGFILE 2>&1
sleep $NN_SLEEP_TIME
CURRTIME=`date +%s`
echo “Current time is $CURRTIME” >> $NN_LOGFILE 2>&1
DIFF=$(($CURRTIME-$LASTOPTIMIZE))
echo “Last optimize is $LASTOPTIMIZE” >> $NN_LOGFILE 2>&1
echo “Difference is $DIFF” >> $NN_LOGFILE 2>&1
if [ “$DIFF” -gt 43200 ] || [ “$DIFF” -lt 1 ]
then
echo “Last optimize is $LASTOPTIMIZE” >> $NN_LOGFILE 2>&1
       LASTOPTIMIZE=`date +%s`
echo “Optimizing stuff” >> $NN_LOGFILE 2>&1
php $NN_OPTIMISE >> $NN_LOGFILE 2>&1
php $NN_UPDATETV >> $NN_LOGFILE 2>&1
php $NN_UPDATETH >> $NN_LOGFILE 2>&1
fi
echo “Loop $LOOPCOUNTER” >> $NN_LOGFILE
done) &
      PID=`echo $!`
      echo $PID > $NN_PID_PATH$PIDFILE
echo “pid is $PID” >> $NN_LOGFILE 2>&1
      echo “Started Newznab binaries update”
      ;;
stop)
      echo “Stopping Newznab binaries update” | tee -a $NN_LOGFILE
      kill -9 `cat $NN_PID_PATH$PIDFILE`
rm $NN_PID_PATH$PIDFILE
      ;;
*)
      echo “Usage: $0 [start|stop]”
      exit 1
esac
user@server:/$ sudo ln -s /var/www/newznab/misc/update_scripts/nix_scripts/newznab_ubuntu.sh /etc/init.d/newznab_ubuntu
user@server:/$ sudo update-rc.d newznab_ubuntu defaults
user@server:/$

One thing to note in the script above: I set NN_SLEEP_TIME to 60 seconds. This seems to decrease the number of script freezes.

Be sure to set the right parameters for the script:

user@server:/$ sudo chmod a+rwx /etc/init.d/newznab_ubuntu
user@server:/$ sudo chown root:root /etc/init.d/newznab_ubuntu 

This script will create a log file, /var/log/newznab_ubuntu.log. If you want this log to get rotated and compressed daly (and you probably do want that) you need to create this file:

user@server:~$ more /etc/logrotate.d/newznab_ubuntu /var/log/newznab_ubuntu.log {
      missingok

      notifempty
      compress
      size 20k
      daily create 0666 root root
}

     echo “Stopping Newznab binaries update” | tee -a $NN_LOGFILE
     kill -9 `cat $NN_PID_PATH$PIDFILE`
rm $NN_PID_PATH$PIDFILE

To start and stop the service use:

user@server:/$ sudo service newznab_ubuntu start
user@server:/$ sudo service newznab_ubuntu stop

I think that is enough to get you to the same point I’m at. If I get Nagios to start stop the service, I’ll update this post.

Posted in apache, automated, cli, command line, monitor, mysql, newznab, php, service, ubuntu • • Top Of Page