I have installed and configured incron on Ubuntu server 16.04. Now I want to monitor if files are added to a specific folder. This works well because when I have

incrontab -l

/var/www/html/uploads IN_ATTRIB touch /home/incronuser/test.txt

and when a file gets added to the uploads folder, the test.txt file gets created in the users home.

Also, if I echo something instead of doing the touch, in both cases I see the incron activity in /var/log/syslog. So that all works as expected.

But now I want to run a bash script when a file is added to the uploads folder. This script works well when the same user that created the incron job does:

~$ bash /home/incronuser/myscript.sh

It runs fine and the script does what it is supposed to do.

Now I change my incron job to run that script when triggered:

incrontab -e 
/var/www/html/uploads IN_ATTRIB /home/incronuser/myscript.sh

But I cannot get this to work for the life of me. Following other Q&A on here and elsewhere on the internet, I have tried using quotes in various forms, tried using the bash command in the crontab line but all have failed up to now. Things like:

/var/www/html/uploads IN_ATTRIB bash /home/incronuser/myscript.sh

/var/www/html/uploads IN_ATTRIB /bin/bash /home/incronuser/myscript.sh

"/var/www/html/uploads IN_ATTRIB /home/incronuser/myscript.sh"

/var/www/html/uploads IN_ATTRIB "/home/incronuser/myscript.sh"

/var/www/html/uploads IN_ATTRIB sudo bash -c "/home/incronuser/myscript.sh"

I have to add that in all these cases, I do see the incron job being called in syslog, just nothing happens. I am not really sure where I could find more detailed logs of what is going wrong. Would be great to get some insight on this.

  • What does your script do? is it executable? what environment variables does it rely on? did you include any logging inside the script itself? – steeldriver Feb 23 '18 at 20:20
  • it needs no env vars. it ibasically starts a mysql script which loads the uploaded file content into a database table. with some cleanup afterwards. But the actual content doesnt appear to be the issue because I just tested that ripping out all the content and doing just a simple touch command fails. I am guessing more something to do with rights or ownership but can't think what it could be. – matv1 Feb 23 '18 at 21:02
  • By default Cron uses limited set of environment variables. Usually (within Ubuntu) its $PATH contains /usr/bin:/bin, so if your script uses commands that are located outside of these directories you should call these commands by using the full path to their locations. – pa4080 Feb 24 '18 at 8:33
  • appreciate both of your thoughts! but the script content really doesnt seem to be the issue. Having tested this by just using a simple touch statement in the script being called, it still fails. the touch command should be found withouth the path given the limitations of cron (and presumably incron) you discribe. But just in case, I tested with the full path to touch as well. But trying to run that script from incron still fails – matv1 Feb 24 '18 at 22:31

Alright I have it sorted out. I will just leave my resolution here for anyone else struggling with this (incron is not as well documented as cron and there are fundamental differences).

The first error I made was the way I constructed my incrontab. This should be:

/var/www/html/uploads IN_ATTRIB /bin/bash /home/incronuser/myscript.sh

Secondly, I wasn't executing it as the right user. The file that needed to be inserted in the database first needed to moved to /var/lib/mysql-files and only root has access there. So the incron job had to be created as root as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.