Running update deamon on Ubuntu server

Howtos, instructions and links to related software. Do not ask questions here.
JanHP
Bear Rating Trainee
Bear Rating Trainee
Posts: 1
Joined: 23 Oct 2012, 17:55

Running update deamon on Ubuntu server

Postby JanHP » 24 Oct 2012, 14:11

Hi,

yesterday I installed Tiny Tiny RSS on my server. One problem I hit was the update daemon. While the Wiki-page on Updating Feeds explicitly describes how to update using cron-jobs, it also mentions that is not the recommended method, so I tried the PHP CLI alternative. Unfortunately, screen seems not to be usable by the Ubuntu "www-data" user, and I before I managed to figure out to install update as a "proper daemon" using start-stop-daemon and init-scripts (it would be nice if someone who does know could post a short howto here), I found a rather simple solution using nohup:

Code: Select all

nohup php -q update.php -daemon -quiet < /dev/null&


Hope it helps,
JAn

miles
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 28 Oct 2012, 15:44

Re: Running update deamon on Ubuntu server

Postby miles » 28 Oct 2012, 15:46

Hi thanks for sharing, could you also say how you setup the init-scripts (and how to run as www-data).

miles
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 28 Oct 2012, 15:44

Re: Running update deamon on Ubuntu server

Postby miles » 28 Oct 2012, 22:26

Try adding this to /etc/rc.local:

Code: Select all

start-stop-daemon -b -c www-data:www-data -S -x /usr/bin/php /path/to/tt-rss/update.php -- -daemon -quiet

liberty
Bear Rating Trainee
Bear Rating Trainee
Posts: 1
Joined: 03 Nov 2012, 04:11

Re: Running update deamon on Ubuntu server

Postby liberty » 03 Nov 2012, 04:48

make a new file, such as ttrss.conf in /etc/init:

Code: Select all

sudo su
cd /etc/init/ && touch trss.conf


Create a script similar to the following:

Code: Select all

description "A script to manage the Tiny Tiny RSS server"

start on filesystem or runlevel [2345]
stop on run level [!2345]

exec start-stop-daemon -S -c www-data:www-data -x /var/www/tt-rss/update_daemon2.php

syzygy
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 17 Mar 2013, 06:17

Re: Running update deamon on Ubuntu server

Postby syzygy » 17 Mar 2013, 06:19

I use the following script for my /etc/init.d/ttrss. You need to chmod +x it, and then start and stop with sudo service ttrss start and sudo service ttrss stop

Code: Select all

#!/bin/sh                                                                                                 

# Starts the tt-rss feed update daemon.                                                                   

set -e

# Must be a valid filename                                                                               
NAME=ttrss
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname                                                   
DAEMON='/usr/bin/php /opt/tt-rss/update.php'
DAEMON_OPTS="-daemon -quiet"

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
cd /opt/tt-rss/

case "$1" in
  start)
        echo -n "Starting daemon: "$NAME
        start-stop-daemon --start --quiet --chuid www-data:www-data --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_OPTS > /dev/null &
        echo "."
        ;;
  stop)
        echo -n "Stopping daemon: "$NAME
        start-stop-daemon --stop --quiet --chuid www-data:www-data --oknodo --pidfile $PIDFILE
        echo "."
        ;;
  restart)
        echo -n "Restarting daemon: "$NAME
        start-stop-daemon --stop --quiet --chuid www-data:www-data --oknodo --retry 30 --pidfile $PIDFILE
        start-stop-daemon --start --quiet --chuid www-data:www-data --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_OPTS > /dev/null &
        echo "."
        ;;

  *)
        echo "Usage: "$1" {start|stop|restart}"
        exit 1
esac

exit 0

syzygy
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 17 Mar 2013, 06:17

Re: Running update deamon on Ubuntu server

Postby syzygy » 17 Mar 2013, 08:58

syzygy wrote:I use the following script for my /etc/init.d/ttrss. You need to chmod +x it, and then start and stop with sudo service ttrss start and sudo service ttrss stop

Code: Select all

#!/bin/sh                                                                                                 

# Starts the tt-rss feed update daemon.                                                                   

set -e

# Must be a valid filename                                                                               
NAME=ttrss
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname                                                   
DAEMON='/usr/bin/php /opt/tt-rss/update.php'
DAEMON_OPTS="-daemon -quiet"

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
cd /opt/tt-rss/

case "$1" in
  start)
        echo -n "Starting daemon: "$NAME
        start-stop-daemon --start --quiet --chuid www-data:www-data --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_OPTS > /dev/null &
        echo "."
        ;;
  stop)
        echo -n "Stopping daemon: "$NAME
        start-stop-daemon --stop --quiet --chuid www-data:www-data --oknodo --pidfile $PIDFILE
        echo "."
        ;;
  restart)
        echo -n "Restarting daemon: "$NAME
        start-stop-daemon --stop --quiet --chuid www-data:www-data --oknodo --retry 30 --pidfile $PIDFILE
        start-stop-daemon --start --quiet --chuid www-data:www-data --pidfile $PIDFILE --exec $DAEMON -- \
$DAEMON_OPTS > /dev/null &
        echo "."
        ;;

  *)
        echo "Usage: "$1" {start|stop|restart}"
        exit 1
esac

exit 0


CORRECTION TO MY PREVIOUS POST! I noticed that /var/run/ttrss.pid wasn't being set in the script above. This caused stop not to work in the script! (I had to kill the daemon manually, with htop in my case). It might be because start-stop-daemon uses www-data, but you need root access to write in /var/run. However, I'm not sure whether running as root is safe, so I went with a screen setup. You can probably also set a different .pid file, like in /opt/tt-rss/run.pid or whatever your www-data can write to (obviously, I installed into /opt/tt-rss). Just FYI

zero_divide_1
Bear Rating Trainee
Bear Rating Trainee
Posts: 3
Joined: 18 Mar 2013, 03:41

Re: Running update deamon on Ubuntu server

Postby zero_divide_1 » 18 Mar 2013, 03:54

If you're using one of the more current Ubuntu server installations, I've created the following Upstart script to handle starting/stopping the daemon:

Code: Select all

description "TT-RSS Feed Daemon"
author "Sean Payne <@zero_divide_1 on Twitter>"

start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [!2345]
respawn

script
        cd /var/www-ttrss
        exec su www-data -c "php ./update_daemon2.php"
end script

How to install it:

    Code: Select all

    sudo nano /etc/init/ttrssdaemon.conf
    and copy-and-paste the script above.
    Create a symlink to the Upstart job:

    Code: Select all

     sudo ln -s /lib/init/upstart-job /etc/init.d/ttrssdaemon

    Finally:

    Code: Select all

    sudo start ttrssdaemon


You can view the log file at (must be root):

Code: Select all

/var/log/upstart/ttrssdaemon.log
.

zero_divide_1
Bear Rating Trainee
Bear Rating Trainee
Posts: 3
Joined: 18 Mar 2013, 03:41

Re: Running update deamon on Ubuntu server

Postby zero_divide_1 » 18 Mar 2013, 04:07

I created the following Upstart script for Ubuntu servers:

Code: Select all

sudo nano /etc/init/ttrssdaemon.conf

Code: Select all

description "TT-RSS Feed Daemon"
author "Sean Payne <@zero_divide_1 on Twitter>"

start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [!2345]
respawn

script
        cd /var/www-ttrss
        exec su www-data -c "php ./update_daemon2.php"
end script

Code: Select all

sudo ln -s /lib/init/upstart-job /etc/init.d/ttrssdaemon

Code: Select all

sudo start ttrssdaemon


The log file can be found at /var/log/upstart/ttrssdaemon

RichardThornton
Bear Rating Trainee
Bear Rating Trainee
Posts: 1
Joined: 20 Mar 2013, 04:47

Re: Running update deamon on Ubuntu server

Postby RichardThornton » 20 Mar 2013, 04:51

This is a great thread and has almost got my issues resolved, thanks! At the moment it appears my daemon kicks off before MySQL can accept the connection; the log has a few mysql_connect() errors before it terminates. Could this be the cause and if so, do you have any suggestions?

jakob42
Bear Rating Trainee
Bear Rating Trainee
Posts: 15
Joined: 20 Mar 2013, 16:43

Re: Running update deamon on Ubuntu server

Postby jakob42 » 20 Mar 2013, 16:46

I modified the "start on" line like this. I haven't restarted my server yet, but it should work:

Code: Select all

start on (local-filesystems and net-device-up IFACE!=lo and started mysql)

zero_divide_1
Bear Rating Trainee
Bear Rating Trainee
Posts: 3
Joined: 18 Mar 2013, 03:41

Re: Running update deamon on Ubuntu server

Postby zero_divide_1 » 22 Mar 2013, 00:11

Sorry for the duplicate posts.

jakob42 wrote:I modified the "start on" line like this. I haven't restarted my server yet, but it should work:

Code: Select all

start on (local-filesystems and net-device-up IFACE!=lo and started mysql)


Good add. I guess it goes without saying that if you are using PostgresSQL instead of MySQL, you'll need to change it accordingly.

gilgongo
Bear Rating Trainee
Bear Rating Trainee
Posts: 3
Joined: 24 Mar 2013, 16:44

Re: Running update deamon on Ubuntu server

Postby gilgongo » 24 Mar 2013, 19:59

Hi - just to say that if anyone's interested, I've put together another init script (before I saw this thread!) that uses screen and doesn't run the daemon as root. I'm using it with Ubuntu 10.04.4 LTS (although it should work with other versions and plain Debian 6+ as well). I have it in /etc/init.d, and set it up to run at boot time with update-rc.d. It should also work with either version of the trss update script.

Code: Select all

#!/bin/bash

### BEGIN INIT INFO
# Provides:  tinyrss
# Required-Start: $local_fs $remote_fs
# Required-Stop:  $local_fs $remote_fs
# Should-Start:   $network
# Should-Stop:    $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:   Tiny Tiny RSS
# Description:    Starts Tiny Tiny RSS update daemon
### END INIT INFO

#Settings
SERVICE='update_daemon2.php'
USERNAME='www-data'
RSSPATH='/path/to_your/trss_dir'
PROCS=`pgrep -d " " -u $USERNAME -f $SERVICE`
#End of settings

ME=`whoami`
as_user() {
  if [ $ME == $USERNAME ] ; then
    bash -c "$1"
  else
    su - $USERNAME -c "$1"
  fi
}

rss_start() {
  if  pgrep -u $USERNAME -f $SERVICE > /dev/null
  then
    echo "$SERVICE is already running!"
  else
    echo "Starting $SERVICE..."
    cd $RSSPATH
    as_user "cd $RSSPATH && screen -h 100 -dmS trss $RSSPATH/$SERVICE"
    sleep 1
    if pgrep -u $USERNAME -f $SERVICE > /dev/null
    then
      echo "$SERVICE is now running."
    else
      echo "Error! Could not start $SERVICE!"
    fi
  fi
}

rss_stop() {
  if pgrep -u $USERNAME -f $SERVICE > /dev/null
  then
    echo "Stopping $SERVICE"
        kill -INT $PROCS       
    sleep 1
  else
    echo "$SERVICE was not running."
  fi
  if pgrep -u $USERNAME -f $SERVICE > /dev/null
  then
    echo "Error! $SERVICE could not be stopped."
  else
    echo "$SERVICE is stopped."
  fi
}

#Start-Stop here
case "$1" in
  start)
    rss_start
    ;;
  stop)
    rss_stop
    ;;
  restart)
    rss_stop
    rss_start
    ;;
  status)
    if pgrep -u $USERNAME -f $SERVICE > /dev/null
    then
      echo "$SERVICE is running."
    else
      echo "$SERVICE is not running."
    fi
    ;;
  *)
  echo "Usage: $0 {start|stop|status|restart|}"
  exit 1
  ;;
esac

exit 0


Thanks - hope this helps somebody.

davedave22
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 28 Mar 2013, 01:08

Re: Running update deamon on Ubuntu server

Postby davedave22 » 28 Mar 2013, 01:13

I tried using the upstart script but I get the error:

PHP Warning: fopen(lock/update_daemon.lock): failed to open stream: Permission denied in /var/www/include/functions.php on line 956
error: Can't create lockfile. Maybe another daemon is already running.

MrBee
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 28 Mar 2013, 18:19

Re: Running update deamon on Ubuntu server

Postby MrBee » 28 Mar 2013, 18:28

I'll throw my two cents in. I'm not the best at writing these things, but it appears to do everything I want. Thoughts and comments appreciated.

The script uses the 'daemon' tool, seems to honor the lockfile (in the 'lock' subdirectory), runs as the apache user, and logs to the 'logs' file under the web root (needs to be created by you).

Code: Select all

#! /bin/sh
# /etc/init.d/ttrssupdater
#


case "$1" in
  start)
    echo "Starting TinyTinyRSS Updater"
    daemon --name=ttrssupdater -F /var/www/lock/update_daemon.lock -u www-data -o /var/www/logs/update_daemon.log /var/www/update_daemon2.php
    ;;
  stop)
    echo "Stopping TinyTinyRSS Updater"
    daemon --name=ttrssupdater -F /var/www/lock/update_daemon.lock -u www-data -o /var/www/logs/update_daemon.log /var/www/update_daemon2.php --stop
    ;;
  restart)
    echo "Restarting TinyTinyRSS Updater"
    daemon --name=ttrssupdater -F /var/www/lock/update_daemon.lock -u www-data -o /var/www/logs/update_daemon.log /var/www/update_daemon2.php --stop
    daemon --name=ttrssupdater -F /var/www/lock/update_daemon.lock -u www-data -o /var/www/logs/update_daemon.log /var/www/update_daemon2.php
    ;;
  *)
    echo "Usage: /etc/init.d/ttrssupdater {start|stop|restart}"
    exit 1
    ;;
esac

exit 0

MrBee
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 28 Mar 2013, 18:19

Re: Running update deamon on Ubuntu server

Postby MrBee » 28 Mar 2013, 19:48

davedave22 wrote:I tried using the upstart script but I get the error:

PHP Warning: fopen(lock/update_daemon.lock): failed to open stream: Permission denied in /var/www/include/functions.php on line 956
error: Can't create lockfile. Maybe another daemon is already running.


Have you checked the permissions for the lock directory? I believe the directories and files beneath them need to be writable by the web server user.


Return to “Knowledge Base”

Who is online

Users browsing this forum: No registered users and 1 guest