1

I am attempting to run a scheduled .py file every 5 minutes with crontab but I don't think it's working.

I'm following this post with the instructions: Can't run python script from cron

I opened up terminal and ran crontab -e, adding in:

5 * * * * /usr/bin/python /home/pi/Desktop/BacTest/test3.py

to the crontab file.

I then saved the crontab with Ctrl-X, then Y, then ENTER.

Nothing appears to happen, even after a reboot... There is a SQLite database file that the test3.py file is supposed to update, but I don't see the updates happening. At least when I right click on the .py file and .db to view the properties there isn't any change to the last access/modification/permission change, or size of the SQLite .db file. However, if I open up terminal in the /home/pi/Desktop/BacTest and run the test3.py file it all works fine...

Any tips are greatly appreciated!

EDIT This is the test3.py file where I am using a python package called BAC0 to access a building automation system. Its really pretty simple where I can retrieve 2 data points (gas usage & outdoor temp) from the hot water system and save to SQLite on pi. BAC0 has a .save() that defaults to SQLite. In terminal on pi when I run the test3.py file it works just fine...

import BAC0
import pandas as pd

bacnet = BAC0.connect(ip='10.30.4.104/16')
hws = BAC0.device('100:99', 30099, bacnet, poll=0)

hws['THERM'].poll()
hws['OA-T'].poll()


print(hws['OA-T'], hws['THERM'])

hws.save()
  • In test3.py have you given the full path to the db file? – joan Sep 19 '18 at 21:20
  • What if you replace '/use/bin/python' with 'python' – Andy Anderson Sep 19 '18 at 21:46
  • Also add an ampersand (&) to the end to allow the task to background. 5 * * * * python ~/Desktop/BacTest/test3.py & – Andy Anderson Sep 19 '18 at 21:49
  • Ill try this... Does it look correct? 5 * * * * python /home/pi/Desktop/BacTest/test3.py & Thanks! – HenryHub Sep 20 '18 at 1:14
  • 1
    The & is pointless in a crontab and you should always give the full path to programs and files in crontab. – joan Sep 20 '18 at 7:23
0

Using:

crontab -e

This works for me:

0 4 * * * sudo /sbin/shutdown -r now    # Reboot everynight at 0400

00,30 06-23 * * * sudo /home/pi/python/scripts/router/DD-WRT_UpgradeCheck.py
  • Gosh still not working on my end... Do you think it has anything to do with the script? But it works just fine if I open up in terminal /home/pi/Desktop/newBAC0 , and run python test.py – HenryHub Sep 26 '18 at 13:50
  • On my crontab program, its just 15 * * * * sudo /home/pi/Desktop/newBAC0/test.py and I used the crontab -e like you recommended and i know it saved properly.. Any tips help! Thank you – HenryHub Sep 26 '18 at 13:51
  • One other thought that ran across my head, is I recently installed berry conda which is Python 3.6 but I think Python 3.5 is still installed on the pi. Would it be possible that 3.5 is running when crontab executes? I dont have the specific packages installed on 3.5 if it is even on the pi... Any idea on how to confirm? – HenryHub Sep 27 '18 at 18:01
  • I think that the different version issue is quite a possibility. Try writing a simple python script that will write the version of Python is using to a file so you will know which version it is using. Or add those packages to 3.5 You might also use Python's "try" & "except" methods to write the error message to a file. That could be quite helpful. – Magwich Sep 28 '18 at 1:25
0

It is possible that the problem lies in the way to call the editor.

If what you want to change is the crontab root, you must edit it with

sudo crontab -e

and with:

crontab -e

the user's

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.