rhaas79
Posts: 2
Joined: Thu Jul 09, 2020 9:33 pm

Trouble with autostart script

Thu Jul 09, 2020 9:43 pm

Greetings! This is my first forum post and I am a relative NOOB when it comes to the Pi. I have a 3b running the latest version of Raspberry Pi OS. I have a script set up to run at startup which checks for network connectivity. If a valid ping of Google occurs then a local web page is opened in kiosk mode. If a valid ping does not occur then the web page does not open. When I execute this script from the command line it behaves as expected. However, when I reboot the Pi and let it run the web page opens in kiosk mode when I do not have a valid network connection. Below is the script I am running:

#!/bin/bash

ping -c 1 8.8.8.8 > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo -u pi chromium-browser --kiosk --incognito http://localhost/index.php
fi

Why does the page open when the network is down when the script is called during boot, but works properly when run from the command line?

hippy
Posts: 7459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Trouble with autostart script

Fri Jul 10, 2020 2:33 pm

rhaas79 wrote:
Thu Jul 09, 2020 9:43 pm
when I reboot the Pi and let it run the web page opens in kiosk mode when I do not have a valid network connection.
That the web page is only opened after the ping reports success suggests either ping is incorrectly reporting success when it should not have, or the network is available when executed, but not when the web page has opened

Perhaps change the script so you can log what ping is doing, what the state of the network appears to be. Something like ...

Code: Select all

#!/bin/bash

ifconfig > /tmp/test.log

ping -c 1 8.8.8.8 >> /tmp/test.log 2>&1
if [ $? -eq 0 ]; then
  echo "Ping Success" >> /tmp/test.log
else
  echo "PIng Failure" >> /tmp/test.log
fi

ifconfig >> /tmp/test.log
Then look at /tmp/test.log to see if that gives any clues.

Interestingly, when I tried that, with a network connected, the output of the ping is placed after the "Success" and output of the second 'ifconfig'. I'm guessing that's just a buffering output issue rather than an actual race condition, but I don't know for sure.

When run from the command line with the ethernet unplugged, I see "connect: Network not reachable" shown on the terminal, but "Success" still shown in the log.

So I guess "ping" then "if [$? -eq 0]" doesn't actually do what you are expecting of it.

The weird thing is this works, shows 0 if network connected, 2 if not ...

Code: Select all

#!/bin/bash
ping -c 1 8.8.8.8 > /dev/null 2>&1
echo $?
But if I add that "echo $?" to the earlier script, immediately after 'ping', it doesn't, always shows 0.

I think you are going to have to wait for a bash expert to come along.

bjtheone
Posts: 770
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: Trouble with autostart script

Fri Jul 10, 2020 4:55 pm

I suspect that something funky is happening with "$?", but I am no where near my reference books and have not been doing bash scripting for a long time.

Some good ideas for testing for connectivity can be found here:

https://unix.stackexchange.com/question ... nnectivity

Going with the simplest... Potentially this might work better, as it eliminates reliance on $?.

Code: Select all

if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
  sudo -u pi chromium-browser --kiosk --incognito http://localhost/index.php
else
  echo "IPv4 is down"
fi

jbudd
Posts: 1358
Joined: Mon Dec 16, 2013 10:23 am

Re: Trouble with autostart script

Fri Jul 10, 2020 5:53 pm

I believe ping exits with status 0 if the network is unreachable, so you cannot use it's exit status to test for network connection.

You could parse the output for packets lost.

Alternatively test the exit status of route --numeric | grep UG
A line in the output with flags U and G is a) Up and b) a Gateway

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Trouble with autostart script

Fri Jul 10, 2020 6:22 pm

This script will report the correct result network up/down based on 2 pings

Code: Select all

 #!/bin/bash

ping -c2 192.168.1.1 
 
if [ $? != 0 ] 
then 
  echo "No network connection"
  
else
    echo "Network up "
    
fi
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

bjtheone
Posts: 770
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: Trouble with autostart script

Fri Jul 10, 2020 6:49 pm

jbudd wrote:
Fri Jul 10, 2020 5:53 pm
I believe ping exits with status 0 if the network is unreachable, so you cannot use it's exit status to test for network connection.

You could parse the output for packets lost.

Alternatively test the exit status of route --numeric | grep UG
A line in the output with flags U and G is a) Up and b) a Gateway
According to the man page:

If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.

bjtheone
Posts: 770
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: Trouble with autostart script

Sat Jul 11, 2020 1:31 am

Hmmmm

Code: Select all

bj@devnull:~ $ if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
> echo "its alive"
> else
> echo "its dead jim"
> fi
its alive

bj@devnull:~ $ if ping -q -c 1 -W 1 1.2.3.4 >/dev/null; then
> echo "its alive"
> else
> echo "its dead jim"
> fi
its dead jim
The defense rests....

DarkElvenAngel
Posts: 750
Joined: Tue Mar 20, 2018 9:53 pm

Re: Trouble with autostart script

Sat Jul 11, 2020 2:01 am

For what you are doing running one command you don't need the if statement at all

Code: Select all

ping -c 1 -q -W 1 8.8.8.8 > /dev/null && sudo -u pi chromium-browser --kiosk --incognito http://localhost/index.php

Return to “General discussion”