1

I am attempting to run a python 3 script every 1 minute using cron on a raspberrypi 3, for testing, where eventually it will just be run once a day.

To start, I made a new cron job using: sudo crontab -e, and typed in the following code for a once a minute job:

*/1 * * * * /home/pi/folder/file.py

Then I saved and closed and waited. My python script emails me text when executed, so I should have seen an email come in. It runs fine (and emails me) when I execute it manually outside of cron.

So, what am I doing wrong with cron for it not to run? And do I need to make the python file executable or something with chmod?

NaN
  • 643
  • 1
  • 8
  • 21
  • Possible duplicate of [Execute python Script on Crontab](https://stackoverflow.com/questions/8727935/execute-python-script-on-crontab) – trejas Dec 24 '17 at 21:12

1 Answers1

1

Possible duplicate of Execute python Script on Crontab

EDIT: Adding comment here since the comment box mangled my formatting.

In your example above it looks like you are just trying to "run" the file. You need to call the python executable, and pass it an argument that points to your file.

From the StackOverflow comment mentioned above look at this crontab entry:

*/2 * * * * /usr/bin/python /home/souza/Documets/Listener/listener.py

Take a look at the first part of the command /usr/bin/python this is pointing to the python executable not just to the .py file you want to run.

trejas
  • 991
  • 7
  • 17
  • I have followed this and it still will not run with cron – NaN Dec 24 '17 at 21:18
  • Is the file executable on the system? (via chmod ?) – trejas Dec 24 '17 at 21:19
  • When you ls -al on the directory what are the permissions on that file? – trejas Dec 24 '17 at 21:24
  • I did make it executable with `chmod a+x file.py`, and when I `ls al` it says `-rwxr-xr-x` – NaN Dec 24 '17 at 21:26
  • In your example above it looks like you are just trying to "run" the file. You need actually call the python executable, and pass it an argument that points to your file. */2 * * * * /usr/bin/python /home/souza/Documets/Listener/listener.py take a look at the first part of the command "/usr/bin/python" this is pointing to the python executable not just to the .py file you want to run. – trejas Dec 24 '17 at 21:31
  • So, when I want to run the .py file I type `python3 file.py` in the terminal. And when I type `which python3` it outputs `/usr/local/bin/python3`. Now, in my cron job if I do `*/2 * * * * /usr/local/bin/python3 /home/pi/folder/file.py`, it still doesn't run, what else am I doing wrong (I appreciate all the help so far)? – NaN Dec 24 '17 at 21:40
  • Can you post the source of your file? Also, have you tried logging some messages to see if the file is executing. It is possible that you have a logic problem that might not be showing up until you try to run it via cron. – trejas Dec 24 '17 at 21:46
  • Unfortunately, I cannot for security reasons, I can only say that it executes when I manually run it using `python3 file.py`. Is there documentation for logging messages? – NaN Dec 24 '17 at 21:49
  • @NaN Your script may run but not correctly as there is a common problem about relative path when you run it with crontab. You can try to create a file with absolute path such as `/home/user/test` to test if your script has been ran. – Sraw Dec 25 '17 at 01:56