【问题标题】:Not able to execute a url from python script running via Cron - Raspberry Pi无法从通过 Cron 运行的 python 脚本执行 url - Raspberry Pi
【发布时间】:2020-08-16 12:42:25
【问题描述】:
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

我可以看到我的代码执行了,但 chronium 浏览器无法打开。有没有人可以帮忙

【问题讨论】:

    标签: python-3.x raspberry-pi python-3.5


    【解决方案1】:

    我和你有同样的问题。您的工作失败了,因为它需要 X session,因为您正在尝试打开 Web 浏览器。您应该将export DISPLAY=:0; 放在您的 cronjob 中的计划之后,如

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

    但请注意,cd Desktop && /usr/bin/python3.5m Morning.py 也可能不适用于 cron。建议将多个命令放入已被chmod +x'd 的.sh 脚本中。

    如果这不起作用,您可以在图形终端中将:0 替换为echo $DISPLAY 的输出。

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      相关资源
      最近更新 更多