在 CronJobs 中正确工作的 Python 模块/库

问题描述 投票:0回答:1

我在使用 cronjob 来执行从 python 脚本调用的 python 模块中的函数时遇到问题。

我目前在 mac OS 上运行它并通过 anaconda 环境运行 python。在阅读了许多 StackOverflow 和 StackExchange 帖子后,我找到了这篇文章(

here

),它对于设置我的 PATH 和 env 变量非常有帮助。能够成功让 python 脚本与 crontab 中指定的作业一起运行。 但是,只有一行脚本(取决于

Pyautogui

模块)未执行。正如大多数帖子提到的,当从终端手动运行时,该脚本运行没有问题,但通过 cron 不会产生相同的结果。

这是我的 crontab,在周一至周五上午 730 点运行;

Pyautogui

这是我的脚本,顶行有 shebang ;超级简单的逻辑,打开一个网址,然后每五分钟刷新该网页,循环 2 小时。

SHELL=/bin/bash HOME=/Users/harrisonw PYTHONPATH=/Users/harrisonw/anaconda3/lib/python3.7/site-packages 30 7 * * 1-5 cd /Users/harrisonw/Documents/cron_jobs && /Users/harrisonw/anaconda3/bin/python3.7 online_status_pyautoguyi.py >> ~/Documents/cron_jobs/online_status_cron_output.txt

#!/Users/harrisonw/anaconda3/bin/python3.7

import os
import time
import pyautogui as py

refresh_counter= 0                  #counter for whileloop to break after certain number
url= "https://www.facebook.com"     #url to access and refresh

os.system("open " + url)            #opens url using os library
time.sleep(10)                      #wait 10 secs for webpage to load

while True:                         #loop refresh command for 2 hours
    time.sleep(300)                 #wait 5 mins 
    py.hotkey('command', 'r')       #calls hotkey function  "Command+R" to refresh page
    print("Refreshed")
    refresh_counter += 1            #count +1 for each refresh
    
    if refresh_counter == 24:       #condition to reach 24 refreshes in 5 min intervals= 2hrs
        break
    else:                           #continue loop if 24 is not reached. 
        continue

print(refreshed_counter)
print("\nComplete") 

行是我寻求帮助的问题。

这是文件中的输出 

py.hotkey('command', 'r')

,如上面的 crobtab 中所述,确认脚本已运行。

online_status_cron_output.txt

我怀疑我缺少 
Refreshed Refreshed 2 Complete

模块的附加路径或 crontab 中的 env 变量,但不知道如何从这里继续。

可能是一个愚蠢的问题,但是 

Pyautogui

与 cronjobs 兼容吗?

对此有任何见解和建议,我们将不胜感激。谢谢!

python cron anaconda environment-variables pyautogui
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.