TransWikia.com

Running python script in incrontab in Debian

Server Fault Asked by WilliamMayor on November 14, 2021

I have a user, dropbox, that runs the Dropbox daemon, I want to monitor the directories in the Dropbox directory for new files and run a python script when they appear.

I have the python script that I know works:

$ /home/dropbox/monitor.py
Trying to get lock
Got lock, waiting for Dropbox to be idle
Dropbox idle
Finding instructions
Done, releasing lock

I have an incrontab entry:

$ incrontab -l
/home/dropbox/Dropbox IN_CREATE /home/dropbox/monitor.py | logger
/home/dropbox/test IN_CREATE logger "$$ $@ $# $% $&"

When I add a file to the test directory I see the output in /var/log/syslog:

$ touch /home/dropbox/test/a
$ tail /var/log/syslog
...
Nov  9 10:18:27 vps incrond[1354]: (dropbox) CMD (logger "$ /home/dropbox/test a IN_CREATE 256")
Nov  9 10:18:27 vps logger: "$ /home/dropbox/test a IN_CREATE 256"
...

However, when I add a file to the Dropbox directory the command doesn’t seem to run:

$ touch /home/dropbox/Dropbox/a
$ tail /var/log/syslog
...
Nov  9 10:24:16 vps incrond[1354]: (dropbox) CMD (/home/dropbox/monitor.py | logger)
...

So the incron daemon notices the new file and the correct command is found to be executed but it never actually gets executed. Nor are there any error messages. It kind of seems like incrontab can only be used to run the most simple of commands.

This might be a similar question to:

but I think that I don’t have env problems, every path is absolute. I tried changing .../monitor.py to /usr/bin/python2.7 .../monitor.py just in case but it didn’t make any difference.

Edit

Dennis Kaarsemaker offered a solution that incrontab was executing my command in a non-cron manner and that this might be causing the problem. Unfortunately, I still can’t get it to work.

First I removed the extra stuff from the incrontab:

$ incrontab -l
/home/dropbox/Dropbox IN_CREATE /home/dropbox/monitor.py

This should run just my monitor file and not try to pass anything in as arguments. No dice, still no output.

Then I created a bash script that contained the execution instruction for my script:

$ vim test.sh
logger "$PATH"
/usr/bin/python2.7 /home/dropbox/monitor.py | logger

This produces the following output:

$ tail /var/log/syslog
Nov  9 23:50:28 vps incrond[1354]: (dropbox) CMD (/home/dropbox/test.sh)
Nov  9 23:50:28 vps logger: /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin

So nothing from monitor.py and we can see that the directory containing python is in incrontab’s environment, so we shouldn’t even need to specify the path as absolutely as we do.

2 Answers

I feel like an idiot for noticing this one.

My monitor.py script attempts to get a file lock before it starts messing around with dropbox stuff. This is mostly to stop other instances of monitor.py from interfering. The path to the file I lock wasn't absolute.

So, lesson to anybody that finds this page in the future. Check every path that you use to make sure that it's either 1) on the path 2) absolute.

Answered by WilliamMayor on November 14, 2021

There's a big difference between cron and incron: the way they execute the commands. Cron sees the command as one argument and calls a shell to run it. Roughly like this:

execvp('/bin/sh', ['/bin/sh', '-c', 'whatever is in the crontab']);

So all shell constructs like && and | work. Incron does not do this, it parses the command itself and execs it directly. So what incron runs in your case is:

execvp('/home/dropbox/monitor.py', ['/home/dropbox/monitor.py', '|', 'logger']);

So your monitor.py gets run with | and logger as arguments. You can either wrap your script in another script that does monitor.py | logger or use the python logging module to do the logging from monitor.py itself.

Answered by Dennis Kaarsemaker on November 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP