Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cronjob Python3 Mac not executing

I just cannot get the cronjob on my Mac to execute. I have following cronjob line:

30 05 * * 1-5 usr/bin/python3 /Users/MyMac/Desktop/hello_world.py

Which should write helloworld to a txt file. This works perfectly when executing directly from the terminal. I insert this line into the crontab file using env EDITOR=nano crontab -e, exit, it says crontab: installing new crontab and when viewing the crontabs with crontab -l it's all there. It just doesn't execute when the time comes. What am I doing wrong?

like image 904
matsbauer Avatar asked Aug 24 '17 03:08

matsbauer


2 Answers

I had the same issue as you, as I commented in your original post, but now I've solved it.

Python3 isn't actually located in usr/bin/Python3, which is why you don't get it to work. The terminal doesn't run the same environmental variables as crontab does which means that even if your path to python3 is wrong it will still manage to run the script from terminal since your user profile has the correct environmental variables set already. The crontab does not have these variables so it locates your script, tries to run it but can't find python3 thus not being able to compile and run the script. Python3 is located in /usr/local/bin/python3.

30 05 * * 1-5 /usr/local/bin/python3 /Users/MyMac/Desktop/hello_world.py

Try this, this works for me. I also recommend testing a software called CronniX. It let's you edit your crontab files without using the terminal which helps a ton. You can also test the script instantly from the software making it easier to see if it works or not (if it works the script should run instantly no matter what schedule times are set for it).

like image 139
ClockWise Avatar answered Nov 11 '22 05:11

ClockWise


I also had the same problem, one of the errors for which my crontab did not work was the path of python3 in my case the path was this: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3

30 10 * * 6 /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 /Users/myMac/Desktop/main.py

Another problem for which it does not work was the permissions, to give all the permissions to cron you must do this:

  1. Go to system preferences, security & privacy, full disk access See the image here

  2. Add a new one See the image here

  3. Go to the folder pressing "Command + shift + G" and paste this "/usr/sbin/cron", search cron and click open. Remember to check cron See the image here


If this doesn't work try giving accessibility permissions:

  1. Go to system preferences, security & privacy, accessibility See the image here

  2. Add a new one See the image here

  3. Go to the folder pressing "Command + shift + G" and paste this "/usr/sbin/cron", search cron and click open. Remember to check cron See the image here

Try this, this worked for me, also remember to give permissions to your terminal.

like image 20
FredyReyes Avatar answered Nov 11 '22 05:11

FredyReyes