Script for checking if device is online, if not then do something
Clash Royale CLAN TAG#URR8PPP
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
bash ip ping
migrated from askubuntu.com Dec 13 at 23:20
This question came from our site for Ubuntu users and developers.
add a comment |
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
bash ip ping
migrated from askubuntu.com Dec 13 at 23:20
This question came from our site for Ubuntu users and developers.
3
What exactly does "stopped working" mean?
– Jaleks
Dec 13 at 23:30
1
What's the purpose of the 0 in0>/dev/null
in the first script? Btw, you don't need to anonymise non routable addresses ;)
– tink
Dec 14 at 0:19
add a comment |
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
bash ip ping
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
bash ip ping
bash ip ping
edited Dec 13 at 23:33
Rui F Ribeiro
38.8k1479129
38.8k1479129
asked Dec 13 at 23:02
diggidre
163
163
migrated from askubuntu.com Dec 13 at 23:20
This question came from our site for Ubuntu users and developers.
migrated from askubuntu.com Dec 13 at 23:20
This question came from our site for Ubuntu users and developers.
3
What exactly does "stopped working" mean?
– Jaleks
Dec 13 at 23:30
1
What's the purpose of the 0 in0>/dev/null
in the first script? Btw, you don't need to anonymise non routable addresses ;)
– tink
Dec 14 at 0:19
add a comment |
3
What exactly does "stopped working" mean?
– Jaleks
Dec 13 at 23:30
1
What's the purpose of the 0 in0>/dev/null
in the first script? Btw, you don't need to anonymise non routable addresses ;)
– tink
Dec 14 at 0:19
3
3
What exactly does "stopped working" mean?
– Jaleks
Dec 13 at 23:30
What exactly does "stopped working" mean?
– Jaleks
Dec 13 at 23:30
1
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 at 0:19
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 at 0:19
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487885%2fscript-for-checking-if-device-is-online-if-not-then-do-something%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
edited Dec 16 at 13:36
Jeff Schaller
38.5k1053125
38.5k1053125
answered Dec 14 at 17:45
diggidre
163
163
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Dec 18 at 16:04
answered Dec 18 at 13:53
JJoao
7,1041828
7,1041828
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487885%2fscript-for-checking-if-device-is-online-if-not-then-do-something%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
What exactly does "stopped working" mean?
– Jaleks
Dec 13 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 at 0:19