Page 1 of 1

How to delay the start of a program

Posted: Mon Mar 05, 2018 8:59 pm
by ilovehd
I have a PI model 3 B running Jessie that auto starts chromium and loads a webpage using:

this file is located at: /home/pi/.config/autostart/autoChromium.desktop

[Desktop Entry]
Type=Application
Exec=/usr/bin/chromium-browser --incognito --noerrdialogs --kiosk http://websiteaddress.com
X-GNOME-Autostart-enabled=true
Name[en_US]=AutoChromium
Name=AutoChromium
Comment5=Start Chromium when GNOME Starts


That works great! My issue is that sometimes this launches before wifi has connected and therefore gives a page cannot be displayed error. Is there any way to add a delay into the above so the wifi has a chance to connect before the browser loads? I've tried just adding sleep 20 above the Exec command but that causes it to not load at all..

Thanks!

Re: How to delay the start of a program

Posted: Tue Mar 06, 2018 12:00 am
by pcmanbob
Have you tried enabling wait for network at boot under boot options in raspi-config

Re: How to delay the start of a program

Posted: Tue Mar 06, 2018 12:45 am
by klricks
Something like this has worked for me:

Make a bash script file for example my_file

Code: Select all

nano /home/pi/my_file

Code: Select all

sleep 3
chromium-browser --incognito --noerrdialogs --kiosk http://websiteaddress.com
Add this line to autostart:

Code: Select all

@bash /home/pi/my_file &
Adjust sleep time if necessary.

I don't know if this will work using the exec= line in the desktop file????? I have never tried that method.

Re: How to delay the start of a program

Posted: Tue Mar 06, 2018 1:24 am
by KLL
this seems to work too

nano /home/pi/.config/autostart/myautostart.sh

Code: Select all

#!/bin/bash
# /home/pi/.config/autostart/myautostart.sh
# this will not do anything unless called by a .desktop file

echo '10s'
env sleep 5
echo '5s'
env sleep 5
#...


/usr/bin/chromium-browser --incognito --noerrdialogs --kiosk http:///www.raspberrypi.org/forums/

# wait for operator to close window ( only for first test / not visible if browser starts )
read -p "press enter to close terminal window"
possibly there is a way to ask for the ready internet connection?


nano /home/pi/.config/autostart/myautostart.desktop

Code: Select all

[Desktop Entry]
Type=Application
Name=browser kiosk
Exec=lxterminal -e /home/pi/.config/autostart/myautostart.sh
Icon=leafpad
Comment=start browser after time delay
Terminal=true
X-KeepTerminal=false
Categories=Network;

Re: How to delay the start of a program

Posted: Tue Mar 06, 2018 11:52 am
by ilovehd
pcmanbob wrote:
Tue Mar 06, 2018 12:00 am
Have you tried enabling wait for network at boot under boot options in raspi-config
This seems to work. I totally missed this in the config, Thank you!

Now the next question do you know how to turn this option on from the command line? I have a few of these that I would like to SSH into and turn this on.

Thanks!

Re: How to delay the start of a program

Posted: Tue Mar 06, 2018 1:17 pm
by klricks
ilovehd wrote:
Tue Mar 06, 2018 11:52 am
pcmanbob wrote:
Tue Mar 06, 2018 12:00 am
Have you tried enabling wait for network at boot under boot options in raspi-config
This seems to work. I totally missed this in the config, Thank you!

Now the next question do you know how to turn this option on from the command line? I have a few of these that I would like to SSH into and turn this on.

Thanks!
sudo raspi-config

Re: How to delay the start of a program

Posted: Tue Mar 06, 2018 1:47 pm
by n67
Comments:

1) I usually do this sort of thing in code, rather than rely on frequently changing obscure raspi-config options. And anything based on hard coded delays (sleep XXX) will, eventually fail because the wait wasn't long enough OR because the wait was too long and the user thinks that nothing is happening (i.e., the machine is "hung"). To do it right, you need to actually check the thing you are waiting for. In this case, the most straightforward way is to check if pinging a known host works. Something like:

Code: Select all

until ping -4 -c1 google.com 2> /dev/null 
do sleep 2
done
Note: If you know it, you could use your router's IP (or, for that matter, any other host on your LAN) rather than google - to avoid putting traffic on the global Internet.

2) If you still want to use raspi-config, and don't want to do the steps interactively, then you will need to dig into the raspi-config code (which, luckily, is a shell script, so it is readable/hackable) and figure out what it does to wait for the network. Good luck and have fun!

Re: How to delay the start of a program

Posted: Tue Mar 06, 2018 1:56 pm
by ilovehd
klricks wrote:
Tue Mar 06, 2018 1:17 pm
ilovehd wrote:
Tue Mar 06, 2018 11:52 am
pcmanbob wrote:
Tue Mar 06, 2018 12:00 am
Have you tried enabling wait for network at boot under boot options in raspi-config
This seems to work. I totally missed this in the config, Thank you!

Now the next question do you know how to turn this option on from the command line? I have a few of these that I would like to SSH into and turn this on.

Thanks!
sudo raspi-config

Thanks!