Faruk
Posts: 54
Joined: Sat Sep 08, 2018 7:34 am

Chromium-Browser does not work from crontab -e

Wed Jan 23, 2019 1:54 pm

Hello

I work for do kiosk with raspberry pi. If rpi connect to internet, chrome shows page from internet But if there is not internet connection on pi, chrome shows the page I previously saved(page.html). I create shell script for check internet connection(online or offline status control). And I want run this shell script every hours check for net connection. I use 'crontab -e' for run this shell script. Also I created log file and I can see error message here. Shell script when start from 'crontab -e' I see this error my log file:

Code: Select all

 --disable-quic --enable-tcp-fast-open --disable-gpu-compositing --ppapi-flash-path=/usr/lib/chromium-browser/libpepflashplayer.so --ppapi-flash-args=enable_stagevideo_auto=0 --ppapi-flash-version=
 [1861:1861:0123/142106.028318:ERROR:browser_main_loop.cc(670)] Failed to put Xlib intothreaded mode.
 (chromium-browser:1861): Gtk-WARNING **:cannot open display:

My script(check.sh):

Code: Select all

#!/bin/sh

wget -q --spider http://google.com

if [ $? -eq 0 ]; then
	killall chromium-browser
	chromium-browser --noerrdialogs http://www.google.com --disable-translate --window-size=1920,1120 --window-position=0,930
else
	killall chromium-browser
	chromium-browser /home/pi/Downloads/page.html --window-size=1920,1120 --window-position=0,930
fi
exit 

My crontab -e file:

Code: Select all

.
.
#m h dom mon dow command
13 14 * * * /home/pi/check.sh >> /home/pi/Desktop/log.txt 2>&1
I searched this error. I tried suggestions(e.g.'DISPLAY=:0.0') but result didn't change. Any suggestion different for solution?

Note: I did 'sudo chmod +x check.sh ' and runned on console it is work good.

User avatar
DougieLawson
Posts: 39892
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Chromium-Browser does not work from crontab -e

Wed Jan 23, 2019 8:37 pm

You can't start GUI programs from cron.

Add a line item to /home/pi/.config/lxsession/LXDE-pi/autostart to get the browser started when the GUI comes up active.

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@/bin/bash /home/pi/check.sh
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Faruk
Posts: 54
Joined: Sat Sep 08, 2018 7:34 am

Re: Chromium-Browser does not work from crontab -e

Thu Jan 24, 2019 4:50 am

So how to control every hour from autostart file. Should the control mechanism be in the sh file?

Faruk
Posts: 54
Joined: Sat Sep 08, 2018 7:34 am

Re: Chromium-Browser does not work from crontab -e

Thu Jan 24, 2019 5:41 am

Additionally tried python script.

Code: Select all

#!/usr/bin/env python  
import requests
import os
import webbrowser
import time

url='www.google.com'
url2='/home/pi/Downloads/page.html'
netflg=True

def internet_on():
    while True:
        try:
            response = requests.get('https://www.google.com/')
            netflg=True
            print(netflg)
            chrome_path='/usr/bin/chromium-browser %s' 
            webbrowser.get(chrome_path).open(url)
            loop()
            return True
            
        except:
            netflg=False
            print(netflg)
            chrome_path='/usr/bin/chromium-browser %s'
            webbrowser.get(chrome_path).open(url2)
            loop()
            return False

def loop():
    time.sleep(10)
    internet_on()
    

internet_on()
and started this script from /home/pi/.config/lxsession/LXDE-pi/autostart. I used

Code: Select all

@/usr/bin/python /home/pi/Desktop/2.py
This time chrome not open every ten second, it starts just one time. When run the script manually, it is work but when I autostart, script has wrong something. Also I check

Code: Select all

ps aux | grep 2.py 
and script is running.
Last edited by Faruk on Thu Jan 24, 2019 7:59 am, edited 2 times in total.

User avatar
DougieLawson
Posts: 39892
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Chromium-Browser does not work from crontab -e

Thu Jan 24, 2019 7:20 am

Faruk wrote:
Thu Jan 24, 2019 4:50 am
So how to control every hour from autostart file. Should the control mechanism be in the sh file?
You can't do it from cron. One possible way may be with https://docs.python.org/3/library/sched.html
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Faruk
Posts: 54
Joined: Sat Sep 08, 2018 7:34 am

Re: Chromium-Browser does not work from crontab -e

Thu Jan 24, 2019 7:58 am

Ok. Finally what is wrong other way. Why python script just work one time?

DirkS
Posts: 10421
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Chromium-Browser does not work from crontab -e

Thu Jan 24, 2019 10:36 am

Faruk wrote:
Thu Jan 24, 2019 7:58 am
Ok. Finally what is wrong other way. Why python script just work one time?
That's unrelated to your original request. Please use 1 issue per topic. That also gives you a better chance of getting a meaningful response.
Start a new topic for this in the Python section

bls
Posts: 788
Joined: Mon Oct 22, 2018 11:25 pm
Location: Seattle, WA
Contact: Twitter

Re: Chromium-Browser does not work from crontab -e

Sat Jan 26, 2019 7:11 pm

One approach that would work is to use a systemd-based service. I created a service file /usr/lib/systemd/user/xload.service containing:

Code: Select all

[Unit]
Description=xload

[Service]
ExecStart=/usr/bin/xload -display myhost.mydomain.com:1
And when I started the service with: systemd start --user xload.service xload displayed on my X windows screen.

You can have as much, or more control with systemd services than with cron, which is really time-based. If you want to restart the browser on a regular basis, the service can be made restartable, and you can have a cron job that runs on a time-schedule to kill the browser, causing systemd to restart it.
Pi tools:
Quickly and easily build customized-just-for-you SD Cards: https://github.com/gitbls/sdm
Easily run your network's DHCP/DNS on a Pi: https://github.com/gitbls/ndm
Easy strongSwan VPN installer/manager: https://github.com/gitbls/pistrong
Lightweight Virtual VNC Config: https://github.com/gitbls/RPiVNCHowTo

Return to “Advanced users”