Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to execute a url from python script running via Cron - Raspberry Pi

import Datetime
import webbrowser
import os
import time

n = datetime.datetime.today().weekday()

fo = open("Morning.py","r")

if n == 0:

    print('Monday')
    webbrowser.open('https://www.youtube.com',new=0)
    time.sleep(10)
    os.system('pkill chromium')

if n == 1:

    print('Monday')
    webbrowser.open('https://www.youtube.com',new=0)
    time.sleep(10)
    os.system('pkill chromium')

if n == 2:

    print('Monday')
    webbrowser.open('https://www.youtube.com',new=0)
    time.sleep(10)
    os.system('pkill chromium')

if n == 3:

    print('Monday')
    webbrowser.open('https://www.youtube.com',new=0)
    time.sleep(10)
    os.system('pkill chromium')

if n == 4:

    print('Monday')
    webbrowser.open('https://www.youtube.com',new=0)
    time.sleep(10)
    os.system('pkill chromium')

if n == 5:

    print('Monday')
    webbrowser.open('https://www.youtube.com',new=0)
    time.sleep(10)
    os.system('pkill chromium')

if n == 6:

    print('Monday')
    webbrowser.open('https://www.youtube.com',new=0)
    time.sleep(10)
    os.system('pkill chromium')

os.system('pkill chromium')

print('Finish')

fo.close()

Python3 - file executes fine but if I want to schedule to run this code to run every minute, I am using Cron (rasberry pi)

* * * * * cd Desktop && /usr/bin/python3.5m Morning.py >> Output.out

I can see my code executes but chronium browser will not open. Can any of you help

like image 697
Ramkee Avatar asked Jan 31 '26 17:01

Ramkee


1 Answers

I had the same problem as you. Your job fails because it requires an X session, since you're trying to open a web browser. You should place export DISPLAY=:0; after the schedule in your cronjob, as in

* * * * * export DISPLAY=:0; cd Desktop && /usr/bin/python3.5m Morning.py >> Output.out

But note that cd Desktop && /usr/bin/python3.5m Morning.py might also not work with cron. It is advised to just put multiple commands into a .sh script that has been chmod +x'd.

If this doesn't work, you could replace :0 with the output of echo $DISPLAY in a graphical terminal.

like image 152
AtilioA Avatar answered Feb 02 '26 10:02

AtilioA