1

My Raspberry Pi 3 (running debain stretch) is connected to a relay board.

Two Python Scripts are turning it on/off. When I run the script manually it works, when I let crontab do it only "on" seems to work. I can not figure out what I did wrong.

I've read some posts and included a shebang, made the file writable,...

on.py

#!/usr/bin/python3
from gpiozero import OutputDevice
light = OutputDevice(2)
light.on()

off.py

#!/usr/bin/python3
from gpiozero import OutputDevice
light = OutputDevice(2)
light.off()

at "crontab -e" i've written:

0 11 * * * /home/pi/Desktop/off.py
Adam
  • 2,726
  • 1
  • 9
  • 22
Michael
  • 11
  • 1
  • Did you remember to chmod +x them? – Julio P.C. May 26 '19 at 17:48
  • @JulioP.C. It probably wouldn't work started manually either. if not marked executable. Unless it was marked +x for owner but not for all users... – CiaPan May 26 '19 at 17:52
  • He could start it manually without assigning the executable flag, by invoking the python interpreter on the command line. – Julio P.C. May 26 '19 at 18:10
  • Both files are executable for "anyone" (right click -> properties -> permission) I just noticed something. When double clicking the file, I get a window with "execute", "open", .... When I click "execute", it doesn't work. When I click open, Thonny opens, I click the green "run current script" button and it works. Does this help? – Michael May 27 '19 at 07:47

1 Answers1

0

Trap the detailed text of the error, as python sees it. This updated version of your cron should do the trick:

0 11 * * * /home/pi/Desktop/off.py 1> /tmp/myerr.txt 2>&1

...and when it fails again, use a text editor to see if an error message is in /tmp/myerr.txt.

The following suggestions will help if the error is a problem with python locating/executing your gpio library.

One way to tell python the location of the library you're using is with some like this:

PYTHONPATH=/foo/bar/baz python somescript.py somecommand

as detailed in this well-voted answer.

Make sure to use absolute path(s) to files/folders in PYTHONPATH in both your cron invocation and your manual invocation; this will eliminate the possibility that cron can't find your library because of a relative path issue.

As an alternaive to PYTHONPATH, you could use sys.path.append as detailed here.

This link and this link may be helpful for gpiozero installation issus.

  • He is using absolute filepath. Just look at his crontab line – Julio P.C. May 26 '19 at 18:08
  • I edited my answer to clarify where I though the better pathing would help. – Erik Ostermueller May 26 '19 at 18:14
  • Hello, thanks for your answer. I'm not sure if I get what you're saying. Does it mean I don't write `from gpiozero import .....` but `from /usr/lib/python3/dist-packages/gpiozero import......` If that's what you mean: 1. it doesn't work, 2. This doesn't make sense to me. The script "on.py" is working with crontab. Both files are executable for "anyone" (right click -> properties -> permission) – Michael May 27 '19 at 07:36
  • I updated my comment with suggestions to get detailed text of the error, and how to avoid problems with python locating your gpio library. – Erik Ostermueller May 28 '19 at 08:53