User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Launch service at boot (with ability to respawn)

Thu Nov 22, 2012 8:23 am

Hi there,

I'm hoping somebody here can answer what, I hope, is a straightforward question (although google has not been my friend so far).

I have a python script which has the ability to run as a service (by passing "start" and "stop" parameters).

I'm looking to make this script start on boot, stop on halt and respawn if, for some reason, the process is stopped.

From what I've seen, I need to be putting some script in /etc/init.d - and would be fairly comfortable in writing such a script.

Where I am less clear is:
  • It looks like I need to do something to get the relevant links created in /etc/rc*.d - not sure what
  • Which run levels are appropriate for my needs
  • How do I give the service the ability to respawn? (There seems to be a "respawn" option in inittab, but I'm not sure how this interacts with an init.d script)
I'm running Debian image from this site.

Any help greatly appreciated.

Thanks,

el_P

limpens
Posts: 38
Joined: Thu Sep 13, 2012 1:15 pm

Re: Launch service at boot (with ability to respawn)

Thu Nov 22, 2012 11:13 am

No experience with the debian image, but you want to use something like sysvinit and inittab:

http://unixhelp.ed.ac.uk/CGI/man-cgi?inittab+5

Check the section on 'respawn'. Your application will get started by init, no need to add startup scripts in /etc/rc.d/

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Launch service at boot (with ability to respawn)

Thu Nov 22, 2012 12:36 pm

Thanks for this.

If I put a link to the script in inittab, it doesn't seem to say anything about star or stop parameters. Does that mean that all the funky coding that I've found on Google for daemonising a python script isn't necessary? i.e. do I just modify the script so it starts automatically and let the system worry about killing it at halt?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

limpens
Posts: 38
Joined: Thu Sep 13, 2012 1:15 pm

Re: Launch service at boot (with ability to respawn)

Thu Nov 22, 2012 1:24 pm

an application started from (sysv)init(tab) does not need to daemonize. When the application exits, it will be respawned.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Launch service at boot (with ability to respawn)

Fri Nov 23, 2012 4:31 pm

Good to know. Thanks for your help.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
k4gbb
Posts: 52
Joined: Sun Aug 12, 2012 5:33 am
Location: Dunnellon, FL USA - EL88tx

Re: Launch service at boot (with ability to respawn)

Fri Nov 23, 2012 7:31 pm

Take a look at the command update-rc.d.
It places links in the rc(x).d directories so that they start/stop at boot/halt .
Example: update-rc.d ax25 defaults 95 5
places links to start ax25 using default levels, starting at sequence 95 and stopping at 5.

There is also a a directory for the Upstart Process.... /etc/initd/.
Upstart scripts do the same thing as the scripts in /etc/init.d/ but have more control as to when & how.
The Grass may be greener on the other side of the fence, but it still has to be mowed.

User avatar
k4gbb
Posts: 52
Joined: Sun Aug 12, 2012 5:33 am
Location: Dunnellon, FL USA - EL88tx

Re: Launch service at boot (with ability to respawn)

Fri Nov 23, 2012 7:45 pm

I forgot to mention the script to be placed in /etc/init.d.
Here is my ax25 script (shortened)

Code: Select all

#! /bin/bash
# Provided by
# 03/09/11
### BEGIN INIT INFO
# Provides: AX.25 Service Start-up
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Ax.25 initialization
# Description: This script provides the control for the ax.25 Packet Radio Service.
#	       The fine tuning is accomplished via /etc/ax25/ax25-up and /etc/ax25/ax25-down.
#
### END INIT INFO

# /etc/init.d/ax25
#! /bin/bash
# Provided by Charles S Schuman ( k4gbb1@gmail.com )
# 03/09/11
### BEGIN INIT INFO
# Provides: AX.25 Service Start-up
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Ax.25 initialization
# Description: This script provides the control for the ax.25 Packet Radio Service.
#	       The fine tuning is accomplished via /etc/ax25/ax25-up and /etc/ax25/ax25-down.
#
### END INIT INFO
# /etc/init.d/ax25

case "$1" in
  start )

    echo "/etc/init.d/ax25: Starting AX25..."
    /etc/ax25/ax25-up
    ;;

    stop )
    echo "Shutting down AX25..."
    /etc/ax25/ax25-down
    ;;

   restart )
    echo "AX25 Restart"
    /etc/ax25/ax25-down
    sleep 1
    /etc/ax25/ax25-up
    ;;

  status )
    if [ ! -d /proc/sys/net/ax25 ] ; then
      echo "ax25 is down"
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0
The Grass may be greener on the other side of the fence, but it still has to be mowed.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Launch service at boot (with ability to respawn)

Fri Nov 23, 2012 7:54 pm

Thank you.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

audioscience
Posts: 9
Joined: Sat Jan 19, 2013 9:45 pm

Re: Launch service at boot (with ability to respawn)

Tue Jan 29, 2013 1:40 am

Hi there. I have a Python script that I want to autostart on boot up and then respawn if it stops.

It's a looping temperature sensor script that reads data then uploads it to Cosm. After it successfully gathers temp and uploads it, it waits 120 seconds before doing it again. Sometimes, however, it will stop or have an error uploading to Cosm (I'm not sure what the issue is). When it does that, the script stops.

What's the best way to have it autostart at boot and start again when it stops? I've been researching use of update-rc.d (init.d) and inittab. What's the best process for this?

Be gentle, I'm a noob. :) Thanks.

mauflo
Posts: 2
Joined: Tue Jan 29, 2013 6:06 am

Re: Launch service at boot (with ability to respawn)

Tue Jan 29, 2013 7:35 am

Hi,

I've got some trouble. I didn't succeed to Launch my script at boot I read many documents, many tutorials and it seems I miss something or I do something wrong but I didn't see what... Please help.

I want to launch on my RPi (wheezy soft float) an axis2 standalone server when my RPi start. I tried many way to do it the last one is this one :
_script name : axis2
_directory : /etc/init.d/
_script :

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides: axis2
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Launch axis2 server
# Description: Start servlet container axis2 and open access to its services
### END INIT INFO


usage() {
    echo "----------------------------------------------"
    echo "Usage: axis2 (stop|start)"
    echo "----------------------------------------------"
    exit 2
}

if [ -z $1 ]; then
    usage
fi

case $1 in
        stop)
                pkill -P $(pgrep axis2server.sh)
        ;;
        start)
                cd /opt/axis2-1.6.2/bin/
                sh axis2server.sh
        ;;
        *)
                usage
        ;;
esac

exit 0
#end script


I suppose I do not need to add "$remote_fs $syslog" when I use "$network" because network is setup after those two if I'm right.

After I'm using :

Code: Select all

sudo chmod a+x axis2
Finally sudo update-rc.d axis2 start 20 2 3 4 5 . stop 20 0 1 6 .

When I look what is produced by the last command I didn't find my NN (20 here) as I read in the documentation about it (cf.

Code: Select all

man update-rc.d
) [S20 expected S03 finally...]

Even if I suppose all is right until here I can reboot and nothing happen and my page http://localhost:8080/axis2/services/ is not accessible. I tried to start my script and it works when I call it in shell.

I'm open and waiting for your reactions, questions, ideas and for sure your solutions if you have one.
thanks !

donkahones
Posts: 1
Joined: Tue Apr 15, 2014 5:52 am

Re: Launch service at boot (with ability to respawn)

Tue Apr 15, 2014 6:34 am

interested if you ever figured out a solution to this issue. I am having the same thing happen and would like to do the same.

mauflo
Posts: 2
Joined: Tue Jan 29, 2013 6:06 am

Re: Launch service at boot (with ability to respawn)

Sat Apr 19, 2014 3:51 pm

Sorry I didn't have any solution yet... I did work on other stuff since this post. I will have a look on it in the next days. Let see !

Return to “General programming discussion”