Page 1 of 2

Help using start-stop-daemon under debian (Raspberry PI)

Posted: 27 May 2013, 14:15
by kaice
Hi,

I am really new to Linux and stuff, but with the help of a friend I managed to run tt-rss on my raspberry Pi. Everything works fine, but my skills are not enough to run the update daemon with the start-stop-daemon function.
I would be deeply grateful, if somebody could explain to me in some steps how I can run the update daemon with the recommended start-stop-daemon. Plus, I would have it run automatically everytime I reboot my Pi.

I found some threads here in the forum, but most of them are to hard for me to follow.

Thanking you in anticipation
Kai

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 27 May 2013, 14:55
by PerryWerneck
Hi,

I'm not sure if the script below will work in raspberry's debian but, it's the one I'm using to start the update as a service in suse linux. You can try it to see if works. If not I can help with some changes.

By the way: Save the file as /etc/init,d/tt-rss

Code: Select all

#!/bin/sh
#
#     Template SUSE system startup script for example service/daemon FOO
#     Copyright (C) 1995--2005  Kurt Garloff, SUSE / Novell Inc.
#         
#     This library is free software; you can redistribute it and/or modify it
#     under the terms of the GNU Lesser General Public License as published by
#     the Free Software Foundation; either version 2.1 of the License, or (at
#     your option) any later version.
#              
#     This library is distributed in the hope that it will be useful, but
#     WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.
#
#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA     
#
# /etc/init.d/FOO
#   and its symbolic link
# /(usr/)sbin/rcFOO
#
# Template system startup script for some example service/daemon FOO
#
# LSB compatible service control script; see http://www.linuxbase.org/spec/
#
# Note: This template uses functions rc_XXX defined in /etc/rc.status on
# UnitedLinux/SUSE/Novell based Linux distributions. If you want to base your
# script on this template and ensure that it works on non UL based LSB
# compliant Linux distributions, you either have to provide the rc.status
# functions from UL or change the script to work without them.
# See skeleton.compat for a template that works with other distros as well.
#
### BEGIN INIT INFO
# Provides:          ttrss
# Required-Start:    $network $remote_fs mysql
# Should-Start:
# Required-Stop:     $network $remote_fs
# Should-Stop:       
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Tiny Tiny RSS update service
# Description:       Start Tiny Tiny RSS
#   Web-based news feed aggregator, designed to allow you to read news from any location,
#   while feeling as close to a real desktop application as possible.### END INIT INFO
#
# Any extensions to the keywords given above should be preceeded by
# X-VendorTag- (X-UnitedLinux- X-SuSE- for us) according to LSB.
#
# Notes on Required-Start/Should-Start:
# * There are two different issues that are solved by Required-Start
#    and Should-Start
# (a) Hard dependencies: This is used by the runlevel editor to determine
#     which services absolutely need to be started to make the start of
#     this service make sense. Example: nfsserver should have
#     Required-Start: $portmap
#     Also, required services are started before the dependent ones.
#     The runlevel editor will warn about such missing hard dependencies
#     and suggest enabling. During system startup, you may expect an error,
#     if the dependency is not fulfilled.
# (b) Specifying the init script ordering, not real (hard) dependencies.
#     This is needed by insserv to determine which service should be
#     started first (and at a later stage what services can be started
#     in parallel). The tag Should-Start: is used for this.
#     It tells, that if a service is available, it should be started
#     before. If not, never mind.
# * When specifying hard dependencies or ordering requirements, you can
#   use names of services (contents of their Provides: section)
#   or pseudo names starting with a $. The following ones are available
#   according to LSB (1.1):
#   $local_fs      all local file systems are mounted
#            (most services should need this!)
#   $remote_fs      all remote file systems are mounted
#            (note that /usr may be remote, so
#             many services should Require this!)
#   $syslog         system logging facility up
#   $network      low level networking (eth card, ...)
#   $named         hostname resolution available
#   $netdaemons      all network daemons are running
#   The $netdaemons pseudo service has been removed in LSB 1.2.
#   For now, we still offer it for backward compatibility.
#   These are new (LSB 1.2):
#   $time         the system time has been set correctly   
#   $portmap      SunRPC portmapping service available
#   UnitedLinux extensions:
#   $ALL         indicates that a script should be inserted
#            at the end
# * The services specified in the stop tags
#   (Required-Stop/Should-Stop)
#   specify which services need to be still running when this service
#   is shut down. Often the entries there are just copies or a subset
#   from the respective start tag.
# * Should-Start/Stop are now part of LSB as of 2.0,
#   formerly SUSE/Unitedlinux used X-UnitedLinux-Should-Start/-Stop.
#   insserv does support both variants.
# * X-UnitedLinux-Default-Enabled: yes/no is used at installation time
#   (%fillup_and_insserv macro in %post of many RPMs) to specify whether
#   a startup script should default to be enabled after installation.
#   It's not used by insserv.
#
# Note on runlevels:
# 0 - halt/poweroff          6 - reboot
# 1 - single user         2 - multiuser without network exported
# 3 - multiuser w/ network (text mode)  5 - multiuser w/ network and X11 (xdm)
#
# Note on script names:
# http://www.linuxbase.org/spec/refspecs/LSB_1.3.0/gLSB/gLSB/scrptnames.html
# A registry has been set up to manage the init script namespace.
# http://www.lanana.org/
# Please use the names already registered or register one or use a
# vendor prefix.

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance

SRVC_BIN=/srv/www/htdocs/tt-rss/update.php
test -x $SRVC_BIN || { echo "$SRVC_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

# Source LSB init functions
# providing start_daemon, killproc, pidofproc,
# log_success_msg, log_failure_msg and log_warning_msg.
# This is currently not used by UnitedLinux based distributions and
# not needed for init scripts for UnitedLinux only. If it is used,
# the functions from rc.status should not be sourced or used.
#. /lib/lsb/init-functions

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     be verbose in local rc status and clear it afterwards
#      rc_status -v -r  ditto and clear both the local and overall rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear both the local and overall rc status
#      rc_exit          exit appropriate to overall rc status
#      rc_active        checks whether a service is activated by symlinks
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0     - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

case "$1" in
    start)
   echo -n "Starting tt-rss update "
   ## Start daemon with startproc(8). If this fails
   ## the return value is set appropriately by startproc.
   /sbin/startproc -q -u wwwrun /usr/bin/php $SRVC_BIN --daemon

   # Remember status and be verbose
   rc_status -v
   ;;
    stop)
   echo -n "Shutting down tt-rss update "
   ## Stop daemon with killproc(8) and if this fails
   ## killproc sets the return value according to LSB.

   /sbin/killproc $SRVC_BIN

   # Remember status and be verbose
   rc_status -v
   ;;

    try-restart|condrestart)
   ## Do a restart only if the service was active before.
   ## Note: try-restart is now part of LSB (as of 1.9).
   ## RH has a similar command named condrestart.
   if test "$1" = "condrestart"; then
      echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
   fi
   $0 status
   if test $? = 0; then
      $0 restart
   else
      rc_reset   # Not running is not a failure.
   fi
   # Remember status and be quiet
   rc_status
   ;;
    restart)
   ## Stop the service and regardless of whether it was
   ## running or not, start it again.
   $0 stop
   $0 start

   # Remember status and be quiet
   rc_status
   ;;
    force-reload)
   $0 try-restart
   rc_status
   ;;

    reload)
   rc_failed 3
   rc_status -v
   ;;

    status)
   echo -n "Checking for service FOO "
   ## Check status with checkproc(8), if process is running
   ## checkproc will return with exit status 0.

   # Return value is slightly different for the status command:
   # 0 - service up and running
   # 1 - service dead, but /var/run/  pid  file exists
   # 2 - service dead, but /var/lock/ lock file exists
   # 3 - service not running (unused)
   # 4 - service status unknown :-(
   # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
   
   # NOTE: checkproc returns LSB compliant status values.
   /sbin/checkproc $SRVC_BIN
   # NOTE: rc_status knows that we called this init script with
   # "status" option and adapts its messages accordingly.
   rc_status -v
   ;;

    probe)
   ## Optional: Probe for the necessity of a reload, print out the
   ## argument to this init script which is required for a reload.
   ## Note: probe is not (yet) part of LSB (as of 1.9)
   ;;

    *)
   echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
   exit 1
   ;;
esac
rc_exit


The updated source is availabel online: https://build.opensuse.org/package/view ... rryWerneck

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 27 May 2013, 15:21
by kaice
Thank you. I will try it when I am at home later.
There is one question I still want to have answered: Why does it need a whole script? I thought it would be enough to use "start-stop-daemon xxx update.php xxx" or something like that. What is the advantage of this script?

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 27 May 2013, 16:42
by PerryWerneck
Hi,

Well, you can always use start-stop from command line but, without the init script you'll have to run the command manually every time the machine boots. Using the standard /etc/init.d script you can manage the update service using the GUI service tools just like the windows service manager.

Not sure if the commands below will work on debian (I'm an opensuse's user), but, after saving the script as /etc/init.d/ttrss you can use the following commands to manage it from command line:

sudo insserv ttrss - Activate the service to run every time the machine boots
sudo service ttrss start - Start the update service
sudo service ttrss stop - Stop the update service

If the service commands doesn't work the direct calls will work:

sudo /etc/init.d/ttrss start - Start the update service
sudo /etc/init.d/ttrss stop - Start the update service

A few notes:

* I just searched google for insserv on raspberry, and, according to the thread in http://www.raspberrypi.org/phpBB3/viewtopic.php?t=7395 the "insserv" command is available in it.

* I Asked to another raspberry owner and, according to him, theres an application named "BUM" (Boot up manager - http://www.ubuntugeek.com/boot-up-manag ... ditor.html) who runs on raspberry to manage system services.

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 27 May 2013, 20:15
by zeiram
Hello

Here are the scripts I use for running the updater on my Raspberry Pi (running Raspbian).

First, create the /etc/init.d/tt-rss file:

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          ttrss-DOMAIN
# Required-Start:    $local_fs $remote_fs networking
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Tiny Tiny RSS update daemon for DOMAIN
# Description:       Update the Tiny Tiny RSS subscribed syndication feeds.
### END INIT INFO

# Author: Pierre-Yves LandurĂ© <[email protected]>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Tiny Tiny RSS update daemon"
NAME=$(command basename "${0}")
DISABLED=0
FORKING=0

# Read configuration variable file if it is present
[ -r "/etc/default/${NAME}" ] && . "/etc/default/${NAME}"

DAEMON_SCRIPT="update.php --daemon"

if [ "$FORKING" != "0" ]; then
   DAEMON_SCRIPT="update_daemon2.php"
fi

DAEMON=/usr/bin/php
DAEMON_ARGS="${TTRSS_PATH}/${DAEMON_SCRIPT}"
DAEMON_DIR="${TTRSS_PATH}"
PIDFILE="/var/run/${NAME}.pid"
SCRIPTNAME="/etc/init.d/${NAME}"

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

if [ "$DISABLED" != "0" -a "$1" != "stop" ]; then
   log_warning_msg "Not starting $DESC - edit /etc/default/tt-rss-DOMAIN and change DISABLED to be 0.";
   exit 0;
fi

#
# Function that starts the daemon/service
#
do_start()
{
   # Return
   #   0 if daemon has been started
   #   1 if daemon was already running
   #   2 if daemon could not be started
   start-stop-daemon --start --make-pidfile --background --quiet --chuid "www-data" --chdir "$DAEMON_DIR" --pidfile "$PIDFILE" --exec "$DAEMON" --test > /dev/null \
      || return 1

   start-stop-daemon --start --make-pidfile --background --quiet --chuid "www-data" --chdir "$DAEMON_DIR" --pidfile "$PIDFILE" --exec "$DAEMON" -- \
      $DAEMON_ARGS --log /var/log/tt-rss-update.log \
      || return 2
   # Add code here, if necessary, that waits for the process to be ready
   # to handle requests from services started subsequently which depend
   # on this one.  As a last resort, sleep for some time.
}

#
# Function that stops the daemon/service
#
do_stop()
{
   # Return
   #   0 if daemon has been stopped
   #   1 if daemon was already stopped
   #   2 if daemon could not be stopped
   #   other if a failure occurred
   start-stop-daemon --stop --make-pidfile --quiet --chuid "www-data" --retry=TERM/1/KILL/5 --pidfile $PIDFILE --name $NAME
   RETVAL="$?"
   [ "$RETVAL" = 2 ] && return 2
   # Wait for children to finish too if this is a daemon that forks
   # and if the daemon is only ever run from this initscript.
   # If the above conditions are not satisfied then add some other code
   # that waits for the process to drop all resources that could be
   # needed by services started subsequently.  A last resort is to
   # sleep for some time.
   start-stop-daemon --stop --quiet --oknodo --retry=0/1/KILL/5 --exec $DAEMON
   [ "$?" = 2 ] && return 2
   # Many daemons don't delete their pidfiles when they exit.
   rm -f $PIDFILE
   return "$RETVAL"
}


case "$1" in
  start)
   [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
   do_start
   case "$?" in
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
   esac
   ;;
  stop)
   [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
   do_stop
   case "$?" in
      0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
      2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
   esac
   ;;
  restart|force-reload)
   #
   # If the "reload" option is implemented then remove the
   # 'force-reload' alias
   #
   log_daemon_msg "Restarting $DESC" "$NAME"
   do_stop
   case "$?" in
     0|1)
      do_start
      case "$?" in
         0) log_end_msg 0 ;;
         1) log_end_msg 1 ;; # Old process is still running
         *) log_end_msg 1 ;; # Failed to start
      esac
      ;;
     *)
        # Failed to stop
      log_end_msg 1
      ;;
   esac
   ;;
  *)
   echo "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
   exit 3
   ;;
esac

:


Then, set up the daemon defaults in /etc/default/tt-rss (do not forget to adapt the path to your installation):

Code: Select all

## Defaults for Tiny Tiny RSS update daemon init.d script

# Set DISABLED to 1 to prevent the daemon from starting.
DISABLED=0

# Emplacement of your Tiny Tiny RSS installation.
TTRSS_PATH="/var/www/tt-rss"

# Set FORKING to 1 to use the forking daemon (update_daemon2.php) in stead of
# the standard one.
# This option is only available for Tiny Tiny RSS 1.2.20 and over.
FORKING=0


(On my system, both files were created with the root user.)

Finally, create the links that will tell the system to start the updater at startup and to shut it down before reboot:

Code: Select all

sudo insserv tt-rss


And voilĂ , your updater will be automatically started on bootup. If you wish to manually stop or start the daemon, you can use the following commands:

Code: Select all

sudo service tt-rss stop
sudo service tt-rss start


Note: for performance reasons, I had to move the database on another machine than the RPi... (I've only tried with MySQL, I might try again with PostgreSQL to test if it fares better on the RPi.)

Edit: Replaced the manual creation of the rc links with using the insserv command.

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 27 May 2013, 20:19
by zeiram
PerryWerneck wrote:sudo insserv ttrss - Activate the service to run every time the machine boots

Wow... I didn't know this one... way easier than creating the links manually as I usually do. Thanks!

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 28 May 2013, 11:50
by kaice
@zeiram
I did everything as you posted, but when I access ttrss via browser it is still telling me "Update daemon is not running".
I changed the path and I made /etc/init.d/tt-rss executable before using insserv.
sudo service tt-rss start did not help, neither a reboot did.
What did I do wrong?

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 28 May 2013, 13:02
by fox
tt-rss checks for the daemon lockfile in LOCK_DIRECTORY, maybe it can't create it there.

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 28 May 2013, 13:04
by kaice
Addendum:
A friend of mine fixed the problem by adding "rm /var/www/ttrss/lock/*" at the beginning of your script.

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 28 May 2013, 13:54
by zeiram
You can verify if the daemon is running (and probably check its error messages if it can't start) in the file /var/log/tt-rss-update.log

You're right, the /etc/init.d/tt-rss should be executable, I forgot this step in my instructions.

For now, that's all I can do (this, and assuring you it's running without problems on my RPi with the files I've posted ;-) )... I'll have a look once I get home.

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 28 May 2013, 14:36
by PerryWerneck
Hi,

What's the output of the following commands:

Code: Select all

sudo /etc/init.d/tt-rss restart
sudo tail -20 /var/log/messages
sudo grep www-data /etc/passwd


The first one tries to restart the service, the second gets the last service messages to see if something was wrong with the restart and the last one just check if the www-data user is available.

kaice wrote:@zeiram
I did everything as you posted, but when I access ttrss via browser it is still telling me "Update daemon is not running".
I changed the path and I made /etc/init.d/tt-rss executable before using insserv.
sudo service tt-rss start did not help, neither a reboot did.
What did I do wrong?

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 28 May 2013, 16:07
by kaice
Hi,
cannot check at the moment, but it is working for me right now. Just needed to wipe /var/www/ttrss/lock/* everytime the script starts. Maybe it's a permission problem with this path.

Edit: Thank you all very much!

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 29 May 2013, 01:13
by sleeper_service
kaice wrote:Hi,
cannot check at the moment, but it is working for me right now. Just needed to wipe /var/www/ttrss/lock/* everytime the script starts. Maybe it's a permission problem with this path.

Edit: Thank you all very much!


that's really a bad practice, the lock files are there to keep you from starting multiple copies that would step on each other.

the only time it's ok to delete lock files on start is when the system is freshly booted, otherwise, you need to check to see if a daemon is running.

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 31 May 2015, 17:43
by blackdragon1
Hi just got a Pi and don't have much experience so I just wanna give some help (not waste hours (it was more like half an hour) with my mistakes) to anyone that might come across this thread.
Currently using Raspbian.

My first mistake:
When running insserv kept getting and error of "incomplete LSB comment.". When copied the script from zeiram, to txt file there was leading whitespace. Yeah, didn't know it shouldn't have any. Solution was to remove leading whitespace.
My second mistake:
After the previous everything went fine, I tried to test the service stop and start but got this instead "env: /etc/init.d/tt-rss: No such file or directory". This one I liked, since I made the file on windows EOL (End-of-line) was on windows. Solution was to open the file on notepad++ went to Edit > EOL Conversion > Unix/Osx format and saved.

And as a bonus, this makes the file executable:
sudo chmod +x /etc/init.d/tt-rss

Thanks for the script zeiram! It's working like a charm.

Re: Help using start-stop-daemon under debian (Raspberry PI)

Posted: 19 Jan 2017, 09:27
by kulak
init.d script from zeiram no longer works on raspberry pi (raspbian 4.4.38-v7+)

I think it has to do with move to systemd.