trouble getting incron inotify to work

Issue

so after alex answer here are my steps :

creating shell code

[email protected][/]# touch mylog.sh
[email protected][/]# nano mylog.sh

copying the code in the mylog.sh

#!/bin/bash
echo "File $1 created." >> /mylog.log

permission

[email protected][/]# chmod +x mylog.sh

creating the log file

[email protected][/]# touch mylog.log 

opening icron table

incrontab -e

putting new command in

/test/ IN_CREATE mylog.sh [email protected]$#

reloading incron – creating a new file – checking the log file

[email protected][/]# incrontab --reload
requesting table reload for user 'root'...
request done

[email protected][/]# cd test
[email protected][/test]# touch newfile.txt

[email protected][/test]# cd /
[email protected][/]# nano mylog.log

but still empty log file … am i missing something ?


finally calling shell script with full path did the trick
so :

/test/ IN_CREATE /mylog.sh [email protected]$#

Solution

You can usually find the incron logs in /var/log/messages

If you want to log events to a specific file you can use:

/test/ IN_CREATE mylog.sh [email protected]$#

where mylog.sh is a shell script which handles the logging.

#!/bin/bash
echo "File $1 created." >> /home/myuser/filescreated.log

Don’t forget to give execution permission to this shell script by chmod +x mylog.sh

Explanation:
As soon as you start using parameters for your command which you’re calling, you have to put it all into a shell script. Since incron don’t pass the arguments to your command but interprets it as an argument for itself.

Don’t forget to call incrontab –reload after changing the incrontab.

Another example

incrontab -e

/text/ IN_CREATE /home/myuser/mylog.sh [email protected] $#

mylog.sh

#!/bin/bash
echo "$(date) File $2 in $1 created." >> /home/myuser/log.txt

Answered By – Alexander Baltasar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published