[SOLVED] Cron help!

I don’t have RPi.GPIO installed on OSMC but I do have it on a Pi running Raspbian. Your code doesn’t give errors on my machine in the same way as you’re getting:

pi@raspberrypi:~ $ ./s4.py
Traceback (most recent call last):
  File "./s4.py", line 19, in <module>
    gpio.wait_for_edge(7, gpio.RISING)
RuntimeError: Error waiting for edge
pi@raspberrypi:~ $ sudo ./s4.py
Traceback (most recent call last):
  File "./s4.py", line 19, in <module>
    gpio.wait_for_edge(7, gpio.RISING)
RuntimeError: Error waiting for edge

(I left off the shutdown, btw.)

So there’s nothing syntactically wrong with the code.

Can you run grab-logs -A for us please.

Hi Jonathan,

I think for the python script to work with cron, you need to give the imports a full path.

so for your script:

#!/usr/bin/python
# -*- coding: utf-8 -*-

 Import the modules to send commands to the system and access GPIO pins

load_src("RPI.GPIO", "../pathto/RPi.GPIO")
import RPi.GPIO as gpio
load_src("os", "../pathto/os")
import os

Thanks Tom.

http://paste.osmc.io/pezudukuyi

I can reproduce the problem on OSMC:

osmc@osmc:/tmp$ sudo ./test.py
./test.py: 7: ./test.py: import: not found
./test.py: 8: ./test.py: import: not found
./test.py: 12: ./test.py: Syntax error: word unexpected (expecting ")")
osmc@osmc:/tmp$ vi test.py
osmc@osmc:/tmp$ sudo ./test.py
Traceback (most recent call last):
  File "./test.py", line 6, in <module>
    import RPi.GPIO as gpio
ImportError: No module named RPi.GPIO

As you can see I edited the test.py file and got a “sensible” error, whereas the first error matches post #15.

The difference is that the first file has a blank line at the head of the file. Could you check if /usr/local/lib/s4/py starts with a blank line? (It also explains why it thinks the two imports are on lines 7 and 8, not 6 and 7)

You were absolutely right. I have removed the empty line from the beginning of the script, now

sudo /usr/local/bin/s4.py

executes without any errors.

The pyjob.log contains:

sh: 1: shutdown: not found

/sbin/shutdown

You beat me to it, was just going to say the full path to shutdown fixed it.
Job log is now clear and everything works!

Phew…

Thanks so much!

Excellent. It was a bit of a hard grind in this case but we got there, and that’s what counts.

Don’t forget to mark the thread as solved (assuming it is, of course!).