trouble getting incron inotify to work

5.2k Views Asked by At

so after alex answer here are my steps :

creating shell code

root@ip[/]# touch mylog.sh
root@ip[/]# nano mylog.sh

copying the code in the mylog.sh

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

permission

root@ip[/]# chmod +x mylog.sh

creating the log file

root@ip[/]# touch mylog.log 

opening icron table

incrontab -e

putting new command in

/test/ IN_CREATE mylog.sh $@$#

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

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

root@ip[/]# cd test
root@ip[/test]# touch newfile.txt

root@ip[/test]# cd /
root@ip[/]# 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 $@$#
2

There are 2 best solutions below

3
On BEST ANSWER

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 $@$#

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 $@ $#

mylog.sh

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

Following with Alexander's Baltasar answer, you could also have a script that does the redirection, and keep your end scripts free of that logic.

Below std_wrapper.sh:

#!/bin/bash

### FLAGS
set -Eeuo pipefail

### INIT SCRIPT
SCRIPT_FULLNAME=$(basename -- ${0})
usage="usage: ${SCRIPT_FULLNAME} log_file target_script target_file watched_dir event"


## ARGUMENTS
log_file="${1}"
target_script="${2}"
target_file="${3}"
watched_dir="${4}"
event="${5}"

### MAIN
if [ -z "${log_file}" ] || [ -z "${target_script}" ] || [ -z "${target_file}" ]; then
  echo "${usage}" >&2
  exit 1
fi

# do the actual call and apply the redirection:
${target_script} "${target_file}" "${watched_dir}" "${event}" >> "${log_file}" 2>&1
  • make sure the script can be run ($ chmod 770 std_wrapper.sh):

In your incrontab ($ incrontab -e):

/test/ IN_CREATE /path/std_wrapper.sh /path/log/test.create /path/actual_script.sh $# $@ $%

actual_script.sh could look something like this:

#!/bin/bash

### FLAGS
set -Eeuo pipefail

### Input Parameters
filename="${1}"
watched_dir="${2}"
event="${3}"
full_filename="${watched_dir}${filename}"

### Main
dt="$(date '+%d/%m/%YT%H:%M:%S')"
echo "$dt (event:) $event (file:) $filename (dir:) $watched_dir <----- going to process ----->"
echo "sleeping 10 seconds..."
sleep 10
dt="$(date '+%d/%m/%YT%H:%M:%S')"
echo "$dt (event:) $event (full_filename:) $full_filename <----- returning from sleep -->"

Creating two files consecutively (in less than 10 seconds)

$ touch /test/new-file && sleep 5 && touch /test/another-file

Would create a log like this:

$ cat /path/log/test.create
07/11/2022T08:00:50 (event:) IN_CREATE (file:) new-file (dir:) /test/ <----- going to process ----->
sleeping 10 seconds...
07/11/2022T08:00:55 (event:) IN_CREATE (file:) another-file (dir:) /test/ <----- going to process ----->
sleeping 10 seconds...
07/11/2022T08:01:10 (event:) IN_CREATE (full_filename:) /test/new-file <----- returning from sleep -->
07/11/2022T08:01:15 (event:) IN_CREATE (full_filename:) /test/another-file <----- returning from sleep -->