sky99
Posts: 6
Joined: Wed Nov 07, 2012 4:12 pm

problem starting a service

Fri Apr 11, 2014 5:32 am

Hello everyone!
I have a problem with raspbian when creating a service.
I created a script that monitors some sensors and prints
the values on a character LCD module. This is for monitoring
my aquarium.

The script works, if i launch it with sudo python aquaPi8.py,
i get the expected result. I created a service in /etc/init.d/lcd0 (source at the bottom of this message).

i then added the script with
sudo update-rc.d lcd0 start 50 2 3 4 5 . stop 50 0 1 6 .

if i start the script with sudo /etc/init.d/lcd0 start, it works as it should.

However, if i reboot, i get "welcome on AquaPi" on the LCD, then nothing.

At this moment, if i restart the service, it works.

This is the second time i have such a problem : on another pi, i have an LCD also printing
sensors (this works), but also printing the IP address of the Pi. I get the sensors working,
but the IP address is never printed, unless i restart the daemon.

I thought that it could be about the service starting too early, so i did set it up to boot late (80),
i did add a 30s delay before the main loop, but no change.

Does anyone have a clue about this problem?
I didn't worry too much about the IP address for my other project, but for the aquarium, the main
purpose of the pi is to print the sensors values without having anything to do.

Thanks in advance!

Here is the code of my service :

Code: Select all

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

### BEGIN INIT INFO
# Provides:          lcd0
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: lcdscreen daemon
# Description:       lcd screen printing realtime data about the system
### END INIT INFO

# Some things that run always
#touch /var/lock/lcd0

script="/home/pi/code/aquaPi8.py"

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting script lcd0 "
    sudo python $script &
    ;;
  stop)
    echo "Stopping script lcd0 with pid $PID"
    sudo kill -s 9 `ps -eo pid,cmd | grep "python $script" -m 2 | cut -d ' ' -f 1`
    ;;
  *)
    echo "Usage: /etc/init.d/blah {start|stop}"
    exit 1
    ;;
esac
exit 0

User avatar
DougieLawson
Posts: 40167
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: problem starting a service

Fri Apr 11, 2014 8:49 am

When you start a daemon it needs to fork a new process and allow the on from the /etc/init.d script to terminate normally with exit 0.

Otherwise you end up in a mess with hanging processes.

I use the start-stop-daemon command with the -background flag as an ugly bypass for the badly behaved python programs.
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Return to “Raspberry Pi OS”