CentOS 6 service script for update daemon

Howtos, instructions and links to related software. Do not ask questions here.
bobcote
Bear Rating Trainee
Bear Rating Trainee
Posts: 4
Joined: 31 Oct 2012, 06:08

CentOS 6 service script for update daemon

Postby bobcote » 05 Sep 2013, 10:13

Hello all,

I searched the wiki and forum but could not find any working service script.
I finally found something that seems to work quite well.

Source: http://www.rosehosting.com/blog/how-to-set-up-tt-rss-tiny-tiny-rss-on-centos-6/

Create file "/etc/init.d/reader" and write into it:

Code: Select all

#!/bin/bash
## reader daemon
# chkconfig: 345 20 80
# description: reader update daemon
# processname: reader
 
# Source function library.
. /etc/init.d/functions
 
# Define some variables
TT_PATH="/var/www/html/tt-rss/"
 
DAEMON=/usr/bin/php
#DAEMONOPTS="update.php --daemon --feeds"
DAEMONOPTS="update_daemon2.php"
 
NAME=reader
DESC="reader update daemon"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
USER=apache
 
case "$1" in
start)
    printf "%-50s" "Starting $NAME..."
    cd $TT_PATH
 
    if [ ! -f $PIDFILE ]; then
 
        daemon --user=$USER $DAEMON $DAEMONOPTS > /dev/null 2>&1 &
        sleep 5
        PID=`ps aux|grep -vE 'grep|runuser|bash'|grep -w '/usr/bin/php update_daemon2.php'|awk '{print $2}'`
 
        if [ -z "$PID" ]; then
            printf "%s\n" "Fail"
        else
            echo $PID > $PIDFILE
            printf "%s\n" "Ok"
        fi
    fi
;;
status)
    printf "%-50s" "Checking $NAME..."
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -z "`pgrep $PID`" ] && [ "$PID" != "`ps aux|grep -vE 'grep|runuser|bash'|grep -w '/usr/bin/php update_daemon2.php'|awk '{print $2}'`" ]; then
            printf "%s\n" "Process dead but pidfile exists"
        else
            echo "Running"
        fi
    else
        printf "%s\n" "Service not running"
    fi
;;
stop)
    printf "%-50s" "Stopping $NAME"
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        cd $TT_PATH
        kill -HUP $PID 2>/dev/null
        printf "%s\n" "Ok"
        rm -f $PIDFILE
    else
        printf "%s\n" "pidfile not found"
    fi
;;
 
restart)
    $0 stop
    $0 start
;;
 
*)
    echo "Usage: $0 {status|start|stop|restart}"
    exit 1
esac


then make it executable by executing:

Code: Select all

# yum install php-process
# service httpd restart
# chmod +x /etc/init.d/reader
# chkconfig --levels 235 reader on
# service reader start

JustAMacUser
Bear Rating Overlord
Bear Rating Overlord
Posts: 373
Joined: 20 Aug 2013, 23:13

Re: CentOS 6 service script for update daemon

Postby JustAMacUser » 05 Sep 2013, 23:54

Thanks for sharing. I also run TT-RSS on a CentOS 6 install and one thing that comes to mind is your chkconfig settings:

# chkconfig: 345 20 80


On my install this would have the Tiny Tiny daemon launching before the MySQL server is loaded. My CentOS 6 install puts mysqld launching at priority 64 and shutting down at 36. Changing the TT-RSS daemon to something like the following might be safer:

# chkconfig: 345 86 14


This way MySQL and Apache launch first. MySQL is obviously required for the TT-RSS daemon so with this mysqld launches well before. Apache is irrelevant but at boot it seems to me that it's better to have the web server up and running before bothering to check feeds.

PostgreSQL may be different. I don't use it because I need MySQL for other applications. Just thought I would mention this though.

Edit:

On another note:

# chkconfig --levels 235 reader on


Specifying --levels isn't necessary since you've declared it in the script already; however, if you're going to, don't add level 2, it's for booting without network access. Launching the TT-RSS daemon when it is incapable of connecting to any web sites is unnecessary.

basica
Bear Rating Trainee
Bear Rating Trainee
Posts: 7
Joined: 27 Apr 2014, 11:49

Re: CentOS 6 service script for update daemon

Postby basica » 27 Apr 2014, 12:09

I've made some minor tweaks to the script and its output to suit my own personal fancy. I've attached the code and a sample of its output here. I can confirm that it works for CentOS 6.5.

Note, the only real difference is the status is highlighted red/green when starting/stopping etc, the name is changed from reader to tt-rss and it will notify you if you're attempting to start it again despite it already running. Despite that, thought I might share it here and confirm it's still working for people.

Code: Select all

#!/bin/bash
## tt-rss daemon
# chkconfig: 345 86 14
# description: tt-rss update daemon
# processname: tt-rss
 
# Source function library.
. /etc/init.d/functions
 
# Define some variables
TT_PATH="/var/www/html/tt-rss/"
 
DAEMON=/usr/bin/php
#DAEMONOPTS="update.php --daemon --feeds"
DAEMONOPTS="update_daemon2.php"
 
NAME=tt-rss
DESC="tt-rss update daemon"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# set this to USER=nginx if you're using nginx instead of apache
USER=apache
 
case "$1" in
start)
    cd $TT_PATH
 
    if [ ! -f $PIDFILE ]; then
         
        printf "%-50s" "Starting $NAME..."
       
        daemon --user=$USER $DAEMON $DAEMONOPTS > /dev/null 2>&1 &
        sleep 5
        PID=`ps aux|grep -vE 'grep|runuser|bash'|grep -w '/usr/bin/php update_daemon2.php'|awk '{print $2}'`
 
        if [ -z "$PID" ]; then
            printf "[ \e[31mFAIL\033[0m ]\n"
        else
            echo $PID > $PIDFILE
            printf "[ \e[32mOK\033[0m ]\n"
        fi
    else
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`pgrep $PID`" ] && [ "$PID" != "`ps aux|grep -vE 'grep|runuser|bash'|grep -w '/usr/bin/php update_daemon2.php'|awk '{print $2}'`" ]; then
                printf "%s\n" "Process dead but pidfile exists"
            else
                printf "$NAME is already running!\n"
            fi
        fi
    fi     
;;
status)
    printf "%-50s" "Checking $NAME..."
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -z "`pgrep $PID`" ] && [ "$PID" != "`ps aux|grep -vE 'grep|runuser|bash'|grep -w '/usr/bin/php update_daemon2.php'|awk '{print $2}'`" ]; then
            printf "%s\n" "Process dead but pidfile exists"
        else
            printf "[ \e[32mRUNNING\033[0m ]\n"
        fi
    else
            printf "[ \e[31mSTOPPED\033[0m ]\n"
    fi
;;
stop)
    printf "%-50s" "Stopping $NAME"
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        cd $TT_PATH
        kill -HUP $PID 2>/dev/null
        printf "[ \e[32mOK\033[0m ]\n"
        rm -f $PIDFILE
    else
        printf "[ \e[31mFAIL\033[0m ]\n"
    fi
;;
 
restart)
    $0 stop
    $0 start
;;
 
*)
    echo "Usage: $0 {status|start|stop|restart}"
    exit 1
esac

Image
Last edited by basica on 28 Apr 2014, 17:36, edited 3 times in total.

venohm
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 28 Apr 2014, 15:01

Re: CentOS 6 service script for update daemon

Postby venohm » 28 Apr 2014, 15:14

Thanks for the update, I appreciate it. I've been wanting this, but has just not bubbled to the top of my todo list.

One change I wanted to point out as well, youir user is nginx instead of apache. Took me a second to figure out why it wasn't working. What I get for changing a script while having my first cup of coffee.

Again, thanks for the update.

basica
Bear Rating Trainee
Bear Rating Trainee
Posts: 7
Joined: 27 Apr 2014, 11:49

Re: CentOS 6 service script for update daemon

Postby basica » 28 Apr 2014, 17:33

No problems. I updated the code with a comment so people have a better chance of noticing it, thanks for pointing it out - figure more people are using apache instead nginx :)

venohm
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 28 Apr 2014, 15:01

Re: CentOS 6 service script for update daemon

Postby venohm » 28 Apr 2014, 19:45

Took me longer than I care to admit to figure out what the problem was. After I finished my first cup of coffee and my brain woke up enough to run a diff on the two, I noticed the user change, so glad to add my little bit of help. Especially since this thread meant I didn't have to write the script from scratch. :D

kevq
Bear Rating Trainee
Bear Rating Trainee
Posts: 2
Joined: 02 Jul 2013, 20:24

Re: CentOS 6 service script for update daemon

Postby kevq » 17 Dec 2014, 11:04

This is perfect, looks to be just what I was looking for. I was wondering, once this is implemented, will the new reader service auto-start on reboot, or will it need to be manually started on boot? If it's the latter, is there a quick and easy way of making it auto-start?

Thanks,

Kev

JustAMacUser
Bear Rating Overlord
Bear Rating Overlord
Posts: 373
Joined: 20 Aug 2013, 23:13

Re: CentOS 6 service script for update daemon

Postby JustAMacUser » 18 Dec 2014, 03:54

Once the script is installed, run:

Code: Select all

sudo chkconfig tt-rss on


Where tt-rss is whatever you've named the process/script. This will cause it to launch at reboot.

Mayhemer
Bear Rating Trainee
Bear Rating Trainee
Posts: 8
Joined: 25 Jan 2014, 15:50

Re: CentOS 6 service script for update daemon

Postby Mayhemer » 06 Feb 2015, 16:21

This init script does not work very good for me.
So I wrote a new script I want to share with you.

I also created a github repository with the script: https://github.com/usr42/ttrss-init.d-centos

The script uses daemonize which can be installed from EPEL to run the php script in the background.
My script uses functions from /etc/init.d/functions like other CentOS services. It does not need a sleep so it starts the service much faster. It is also possible to configure log files (variables LOGFILE and ERRLOGFILE). The log files must be writable for the user configured in the USER variable (here apache).

Create file "/etc/init.d/tt-rss":

Code: Select all

#!/bin/bash
## tt-rss daemon
# chkconfig: 345 86 14
# description: tt-rss update daemon
# processname: tt-rss
 
# Source function library.
. /etc/init.d/functions
 
# Define some variables
TT_PATH="/var/www/tt-rss"
PHPBIN=/usr/bin/php
UPDATESCRIPT="$TT_PATH/update_daemon2.php"
LOGFILE=/dev/null
ERRLOGFILE=/dev/null
#LOGFILE=/var/log/tt-rss-update.log
#ERRLOGFILE=/var/log/tt-rss-update-error.log
USER=apache
 
NAME=tt-rss-updater
DESC="tt-rss update daemon"
PIDFILE=/var/run/$NAME.pid

start() {
  echo -n $"Starting $NAME"
  daemonize -u $USER -p $PIDFILE -o $LOGFILE -e $ERRLOGFILE -c $TT_PATH $PHPBIN $UPDATESCRIPT && success || failure
        RETVAL=$?
  echo
        return $RETVAL
}

stop() {
  echo -n $"Stopping $NAME"
  killproc -p $PIDFILE $NAME
  RETVAL=$?
  echo
  return $RETVAL
}

rh_status() {
  status -p $PIDFILE $NAME
}

rh_status_q() {
        rh_status >/dev/null 2>&1
}

case "$1" in
  start)
                rh_status_q && echo $NAME is already running && exit 0
    start
    ;;

  status)
    rh_status
    ;;

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


The variables at the start of the script may need to be adapted to your needs. After that execute following commands:

Code: Select all

chmod +x /etc/init.d/tt-rss
chkconfig tt-rss on
service tt-rss start


To use the log files change following lines in the script:

Code: Select all

#LOGFILE=/dev/null
#ERRLOGFILE=/dev/null
LOGFILE=/var/log/tt-rss-update.log
ERRLOGFILE=/var/log/tt-rss-update-error.log


After that create the log files with the right access rights:

Code: Select all

touch /var/log/tt-rss-update.log /var/log/tt-rss-update-error.log
chown apache:apache /var/log/tt-rss-update.log /var/log/tt-rss-update-error.log


Have fun with the init script!


Return to “Knowledge Base”

Who is online

Users browsing this forum: No registered users and 0 guests