pythonraspberry-pilirc

lirc - Python counter script will not run


I am puzzled as to why the following script (count.py) will not run using the IR remote

#!/usr/bin/env python
from __future__ import with_statement
with open('num.txt','r+') as f:
   counter = str(int(f.read())+1)
   f.seek(0)
   f.write(counter)

I have other scripts which work fine i.e. this one when mapped to same button executes without issue

#!/usr/bin/env python
import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BCM) ## Use board pin numbering
GPIO.setup(22, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
GPIO.output(22,True) ## Turn on GPIO pin 7
GPIO.cleanup()

The problem script will work from the command line using 'sudo python count.py' and if I start irexec from the command line with sudo then it will also run using the remote. In essence starting irexec in this way is a workable solution. Nevertheless I am still keen to establish why under the present conditions the script will not run.


Solution

  • It might be that the problem script is not able to open "num.txt": either it is running as a user who doesn't have permission, or it is running from a directory you don't expect and so can't see the relative path to num.txt. In this case the script would fail with an error, but this might be being lost somewhere.

    Try checking logs to see if there are any errors reported.

    You can also alter the script to use an absolute path to num.txt, and catch exceptions and write them to a log somewhere (also absolute path, and all write permissions? - maybe in /tmp)