0

Im having an issue with cronjob to execute a function from a python module Pyautogui called from a python script.

Im currently running this on mac OS and running python through anaconda environment. After reading many StackOverflow & StackExchange posts, I was able to find this one (here) that was super helpful in getting my PATHs and env variables set. Was able to successfully get the python script to run with the job specified in the crontab.

However, just one line of the script (dependent on the Pyautogui module) is not executing. As most of the posts mention, this script runs with no issues when manually ran from terminal but does not result the same through cron.

Here is my crontab to run at 730am Mon-Fri;

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 

Here is my script w/ the shebang at the top line ; super simple logic to open a url then refresh that webpage every five minutes for 2 hours on a loop.

#!/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") 

The line py.hotkey('command', 'r') is the issue im seeking help for.

Here is the output in the file online_status_cron_output.txt as stated in the crobtab above which confirms the script was run.

Refreshed
Refreshed
2
Complete

Im suspecting that Im missing an additional PATH to the Pyautogui module or an env variable in the crontab but not sure how to proceed from here.

Might be a silly question but is Pyautogui compatible with cronjobs?

Any insight and advise around this is appreciated. Thanks!

HWFord16
  • 19
  • 5
  • That might not work well, since activating an environment involves more than just setting the path for the Python installation. [This answer](https://stackoverflow.com/a/60977676/11301900), and the rest of the post, could be useful. – AMC Sep 28 '20 at 22:00
  • Does this answer your question? [run a crontab job using an anaconda env](https://stackoverflow.com/questions/36365801/run-a-crontab-job-using-an-anaconda-env) – AMC Sep 28 '20 at 22:05
  • Hey there AMC, thanks for the response. I read through and it sort of makes sense being that this example was for Ubuntu. However, i have seen that snippet with the correct reference as seen in the `~/.bashrc` example when i access my `~/.bash_profile` via `vim` on mac OS X. Following the rest of the answer, it seems im missing my `BASH_ENV` variable and the `conda activate my_env` in my crontab. – HWFord16 Sep 29 '20 at 00:00
  • _I read through and it sort of makes sense being that this example was for Ubuntu._ Yes, I would expect it to be similar. – AMC Sep 29 '20 at 00:02
  • Followed the guide and the results were the same as in the script was still able to run and open the url provided but still the hotkey command ```py.hotkey('command', 'r')``` still was not outputting. the ```PYTHONPATH``` to that library is correct after double checking. anything else i can try? – HWFord16 Oct 01 '20 at 04:06
  • Are the contents of `sys.path` what you expect? – AMC Oct 01 '20 at 23:09

0 Answers0