kevlar1818
kevlar1818

Reputation: 3125

incron on Raspbian not working

I have virtually an identical situation as this question, except the accepted answer does not work for me at all. Making this simple Python script is my second attempt; echoing text and redirecting it to a file doesn't do anything either. I am using the Raspbian linux distro.

pi@raspberrypi ~ $ incrontab -l
/home/pi IN_CREATE,IN_DELETE /home/pi/test.py

pi@raspberrypi ~ $ cat test.py 
#! /usr/bin/python3 
f = open('test.txt', 'a+')
f.write('success!\n')
f.close()

pi@raspberrypi ~ $ touch abc.123; rm abc.123

pi@raspberrypi ~ $ tail -n 3 /var/log/syslog
May 17 00:17:09 raspberrypi incrond[1799]: (pi) CMD (/home/pi/test.py )
May 17 00:18:36 raspberrypi incrond[1799]: (pi) CMD (/home/pi/test.py )
May 17 00:18:36 raspberrypi incrond[1799]: (pi) CMD (/home/pi/test.py )

pi@raspberrypi ~ $ ls
bin  Desktop  python_games  test.py

Notice the lack of test.txt in the home directory.

Upvotes: 1

Views: 1221

Answers (1)

Yves Martin
Yves Martin

Reputation: 10361

I have tested on a standard Debian Wheezy. The problem your script faces come from the fact that the current working directory (CWD) is not what you expect.

Setting an absolute path in your open operation is a way to avoid it:

f = open('/home/pi/test.txt', 'a+')

First, I have fear an infinite recursion if events for test.txt changes trigger again the script, but it seems to be handled by incron.

As stderr is lost when triggered by incron, it is important to test the script manually with ./test.py.

Here is a variation of your script with additional information thanks to $@ option:

#! /usr/bin/python3
import sys
import os
f = open('/home/pi/test.txt', 'a+')
f.write('success on ' + sys.argv[1] + ' with CWD=' + os.getcwd() + '\n')
f.close()

Which is registered that way:

$ incrontab -l
/home/pi IN_CREATE,IN_DELETE /home/pi/test.py $@

Now you will see in /home/pi/test.txt

success on /home/pi/ with CWD=/

which explains that your script first tries to write /test.txt and has not required permission on file system to do so.

Upvotes: 3

Related Questions