デバイスがオンラインであることを確認し、そうでない場合はいくつかのタスクを実行するスクリプト

デバイスがオンラインであることを確認し、そうでない場合はいくつかのタスクを実行するスクリプト

私はrtsp ffmpegのキャプチャと年中無休目的のためのIPカメラサーバーを構築しています。欠けている唯一のものは、カメラの接続を確認するスクリプトです。アクセスできない場合は、新しいffmpegキャプチャプロセスを開始できるように、カメラのオンライン状態を確認するために別のスクリプトを起動する必要があります。

私はこれをテストするのに多くの時間を費やしましたが、今は何も機能しません。だから、これには3つのスクリプトがあります。最初はカメラにまだアクセスできることを確認し、そうでない場合は2番目に移動します。

#!/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

2番目のアイテムに再びアクセスできることを確認する必要があります。そうであれば、3番目のアイテムに移動してください。

#!/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        

3番目は新しいffmpegプロセスを開始し、ffmpegとping PIDをファイルに書き込みます(後で必要です)。

#!/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

問題は...最初のスクリプトはうまく実行されますが、2番目のスクリプトには問題があるということです。それからfpingで別のことを試しましたが、運がありませんでした。今、whileループをpingすると完璧に動作します。ところで、最初のスクリプトは動作を停止します。私は奇妙に見えます。

サーバーはRaspbian Stretchを使用するRPI 3b +です。

ベストアンサー1

わかりました、わかりました!この場合、失敗thenはないようですelse。今動作します。

# 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

おすすめ記事