Hey guys,
I'm rather new to scripting with the shell (I started on Wednesday) and encountered a problem that left me clueless.
I moved to an old house recently and the sound of the 433mhz door bell is too quiet. Since I always wanted to start playing around with a raspberry pi and my wife allowed me to get a new toy, I got one.
Now the pi receives the signal from the door bell's sender and activates several wireless sockets around the house, making it impossible to not notice that someone is at the door.
My first script was in php, but the infinity loop waiting for the signal took too much ressources and I wanted to optimize it - so I made some shell scripts.
I made the php script a daemon, because I restart the pi sometimes and didn't want to forget starting the 'bell expander'. That worked well.
Now I made my shell script a daemon and that works not so well. It starts, idles at low cpu load and waits for signals. It receives them, logs the time and such but does not execute the script that controls the wireless sockets. And I have no clue why.
Please have mercy with my script...
First script, started as a daemon, listens for incoming signals, logs some stuff and triggers the second script (well, at least it should...)
/home/pi/tuerklingel_ueberwachung:
#!/bin/bash
TRIGGERED=0
NOW=`date +"%d.%m.%Y, %R:%S"`
RING=0
echo "$NOW Uhr Starte �berwachung nach Reboot" >> /home/pi/tuerklingel.log
exec 3<>/dev/tcp/localhost/5000
printf '{"action":"identify","options":{"receiver":1},"uuid":"0000-00-00-70-161865"}'>&3
read -r RETURN <&3
if [ "${RETURN}" = '{"status":"success"}' ]
then
echo "$NOW Uhr Prozess erfolgreich gestartet" >> /home/pi/tuerklingel.log
else
echo "$NOW Uhr Prozess nicht gestartet, es ist ein Fehler aufgetreten" >> /home/pi/tuerklingel.log
return 0
fi
while true
do
read -u 3 -r RETURN
unit=$(echo $RETURN | jq -r '.message.unit')
id=$(echo $RETURN | jq -r '.message.id')
systemcode=$(echo $RETURN | jq -r '.message.systemcode')
unitcode=$(echo $RETURN | jq -r '.message.unitcode')
NOW=`date +"%d.%m.%Y, %R:%S"`
TIMESTAMP=`date +"%s"`
TIMESTAMP=`expr $TIMESTAMP - 7`
if [ "$TRIGGERED" -lt "$TIMESTAMP" ]
then
if [ "$systemcode" = "2" ] && [ "$unitcode" = "6" ]
then
echo "$NOW Uhr Klingel 1 wurde bet�tigt" >> /home/pi/tuerklingel.log
RING=1
elif [ "$id" = "D7" ] && [ "$unit" = "23" ]
then
echo "$NOW Uhr Klingel am Tor wurde bet�tigt" >> /home/pi/tuerklingel.log
RING=1
elif [ "$id" = "F10" ] && [ "$unit" = "32" ]
then
echo "$NOW Uhr Klingel am Haus wurde bet�tigt" >> /home/pi/tuerklingel.log
RING=1
fi
if [ "$RING" -eq 1 ]
then
/bin/bash /home/pi/tuerklingel_meldung
TRIGGERED=`date +"%s"`
echo "$NOW Uhr Gongs und Lichter ausgel�st" >> /home/pi/tuerklingel.log
RING=0
fi
fi
done
The second script calls all the different scripts that activate/deactivate the wireless sockets. I seperated each of the scripts so that I don't have to restart the daemon when I change something and to control the sockets individually, like how long they are active and so on. Some sleep for a few seconds because they activate flashing lights, for example.
/home/pi/tuerklingel_meldung
tuerklingelbefehle/blaulichtAn & tuerklingelbefehle/lidlGongAn & tuerklingelbefehle/ouboGongAn
And this is the init script:
#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides: Funkklingel-�berwachung
# Required-Start: $remote_fs $syslog $network pilight
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Ausl�sen der Klingel �berwachen
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d. This example start a
# single forking daemon capable of writing a pid
# file. To get other behavoirs, implemend
# do_start(), do_stop() or other functions to
# override the defaults in /lib/init/init-d-script.
### END INIT INFO
# Author: Foo Bar <foobar@baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.
DESC="Ausl�sen der Klingel �berwachen"
DAEMON=/home/pi/tuerklingel_ueberwachung
NAME=tuerklingel
#DAEMON_ARGS="--options args"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/tuerklingel_ueberwachung
do_start_cmd() {
start-stop-daemon --background --start --quiet --pidfile ${PIDFILE} -m \
--startas $DAEMON --name $NAME --test -c 1000 > /dev/null \
|| return 1
start-stop-daemon --background --start --quiet --pidfile ${PIDFILE} -m \
--startas $DAEMON --name $NAME -c 1000 \
|| 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.
}
The weird thing is - it works like a charm when I execute the /home/pi/tuerklingel_ueberwachung manually. But if it runs as a daemon, it doesn't execute /home/pi/tuerklingel_meldung. And I have no idea why...
The daemon runs as the user pi, the user that owns all the scripts. chmod a+x is set for every script.
It doesn't matter how I try to call /home/pi/tuerklingel_meldung, it doesn't work.
I tried
-
. /home/pi/tuerklingel_meldung
-
/home/pi/tuerklingel_meldung
-
/bin/bash /home/pi/tuerklingel_meldung
-
sh /home/pi/tuerklingel_meldung
and possibly some other variations that I don't remember at the moment.
Please help me!
Kind Regards
Matthias