incron on Raspbian not working

左心房为你撑大大i 提交于 2019-12-13 01:34:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16601386/incron-on-raspbian-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!