I've been looking as various ways to fix this issue but cannot find anything that works.
I can see that the script is running by debugging yet the pyautogui function is not running via Cron. I can confirm the script runs as expected via cli and that the pyautogui module can be seen by cron.
This is what my cron job looks like for testing.
* * * * * cd /Users/joeblogs && python3 ./mouse.py >> /Users/joeblogs/debug.txt 2>&1
This the my script i'm using, it's a very simple script that checks the mouse coordinates then wait 5 seconds and checks again. If the coordinates match the the mouse is moved via pyautogui, if they don't match then the program exits.
#!/usr/bin/env python3
import pyautogui
import time
import sys
def MovMouse():
prev = pyautogui.position() #Grabs X,Y mouse position
#print("position X1 is", prev[0])
#print("position Y1 is", prev[1])
time.sleep(5)
after = pyautogui.position() #Grabs second X,Y position after 3 seconds
#print("position X2 is", after[0])
#print("position Y2 is", after[1])
if (prev == after):
print ("!!! MOVE MOUSE !!!")
pyautogui.moveRel(200, 200)
#postmove = pyautogui.position
#print("position X3 is", postmove[0])
#print("position Y3 is", postmove[1])
else:
print("Mouse does not need to be moved")
sys.exit(0)
MovMouse()
I've looked at similar posts like this one but no joy.
Any help would be great as this issue has been bugging me for a while!
FYI I'm using the latest version of python3 as of writing this and running in MacOS.
Solved - I needed to allow accessibility access to cron and my terminal in MacOS settings and now it's working as expexted!