1

I'm building an IP Camera Server for rtsp ffmpeg capture and 24/7 purposes. Only thing that is missing is a script that checks connectivity of the camera and if it's not reachable there should be triggered another script which checks the cam for online status so that a new ffmpeg capture process can be started then.

I already spent plenty of time testing, but nothing will work right now. So for the job I have three scripts. The 1st should check if the camera is still reachable and, if not, then go to the 2nd:

#!/bin/sh
# record-ping_cam1.sh
# Check 24h if cam is alive, in case of error code 1 (offline) start record-waitfor_xxx.sh
#
IPCAM=192.168.xxx.xxx
ping -w 86400 -i2 $IPCAM 0>/dev/null
OFFLINE=$?
if [ $OFFLINE -eq 1 ]
then
  source /home/xxx/record-ping-waitfor_cam1.sh
fi

The 2nd should check if it reachable again and, if it is, then go to the 3rd:

#!/bin/sh
# record-ping-waitfor_cam1.sh
# Check if Cam is alive, if yes (exit code 0) then execute record-ping-reconnect_xxx.sh
#
# Ping with infinitive loop - as soon as reachable (exit code 0) then go on with record script
IPCAM=192.168.xxx.xxx
while true; do ping -c1 $IPCAM > /dev/null && break; done
ONLINE=$?
if [ $ONLINE -eq 0 ]
then
  source /home/xxx/record-ping-reconnect_cam1.sh
fi        

The 3rd starts the new ffmpeg process and writes ffmpeg and ping PIDs to file (needed later):

#!/bin/sh
# record-ping-reconnect_cam1.sh
# Record IPcam after any case of signal lost
#
# This will print the current date and time in a format appropriate for storage
STARTTIME=$(/bin/date +"%d.%m.%Y")-"("$(/bin/date +"%H").$(/bin/date +"%M")Uhr")"
#
## IP Camera Names ##
# Creating date stamps for each of the Cameras
CAM=Cam1_$STARTTIME
#
## Network and Local Storage Locations  ## #Trailing '/' is necessary here
RCDIR="/home/xxx/Reconnect/"
#
## Record Time per File sec ##
LENGTH="86400" # (24h)
#
## Record Settings ##
#
# wait until cam is ready to capture again
sleep 40s
# start capture this camsource
ffmpeg -v 0 -rtsp_transport tcp -i "rtsp://device:port/11" -vcodec copy -an -t $LENGTH $RCDIR$CAM1.mkv & echo $! > /home/xxx/Reconnect/PIDs/ffmpeg_cam1.pid
# start the ping routine, check the cam for connectivity
source /home/xxx/record-ping_cam1.sh & echo $! > /home/xxx/Reconnect/PIDs/ping_cam1.pid
exit

The thing is... the 1st script worked fine but I had trouble with the 2nd. I tried then different things with fping but without luck. Now with ping in the while loop it's working flawlessly. But then the 1st script stopped working... that seems weird to me.

Server is a RPI 3b+ with Raspbian Stretch

migrated from askubuntu.com Dec 13 '18 at 23:20

This question came from our site for Ubuntu users and developers.

  • 3
    What exactly does "stopped working" mean? – Jaleks Dec 13 '18 at 23:30
  • 1
    What's the purpose of the 0 in 0>/dev/null in the first script? Btw, you don't need to anonymise non routable addresses ;) – tink Dec 14 '18 at 0:19
1

Ok figured out! Seems then without else is the failure in this case. Now it's working.

# Ping in an infintive loop - as soon as reachable (exit code 0) then go on with record script
HOST=adress
ping -w 86400 -i2 $HOST 0>/dev/null
OFFLINE=$?
if [ $OFFLINE -eq 1 ]
then
  echo " " 
else
  bash /home/xxx/record-ping-waitfor_g-cam1.sh
fi
0

Just a comment to emphasize that you can use the return code directly in "if"

if ping -w 10 -c2 adress &> /dev/null
then echo "Ok"
else echo "Call the sys admin"
fi

See also the options of ping and redirection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.