Incrontab output to file not working

2k Views Asked by At

I've got an incrontab rule set up to react to a file being added to a particular directory on my ubuntu box, and run a script. This works fine, but my goal is to print the output of that script to a file.

I've tried a few different ways to go about it, and the only way I've been able to get it working so far is to take the generated command that incrontab creates, and run it myself. So my thinking is that its possible, I'm likely just missing something obvious to an experience linux user.

I've shortened some of the commands for brevity's sake. "watchdir" "scriptPath" and "arg1" are all paths. I am bringing in two arguments to the script, arg1, and the filename wildcard from the cron job.

I've tried: (These run the script, but don't output to file)

watchdir IN_CREATE scriptPath arg1 $# >> /home/ubuntu/logs/log-$# 2>&1

watchdir IN_CREATE scriptPath arg1 $# &>> /home/ubuntu/logs/log-$#

(These do nothing at all)

watchdir IN_CREATE /bin/bash scriptPath arg1 $# >> /home/ubuntu/log/log-$# 2>&1

watchdir IN_CREATE /bin/bash scriptPath arg1 $# &>> /home/ubuntu/log/log-$#

If I run 'tail /var/log/syslog' and grab the command generated from the incrontab below and paste it into the shell, it works fine

watchdir IN_CREATE scriptPath arg1 $# &>> /home/ubuntu/logs/log-$#

This works

scriptPath arg1 mission-LHPUQ7ezcF0s0UwVgUR.txt &>> /home/ubuntu/logs/log-mission-LHPUQ7ezcF0s0UwVgUR.txt

Any insight as to what I could be missing?

2

There are 2 best solutions below

1
Nicolas On

I had a similar problem and found out, that incron simply ignores the output redirection and hands them over to the script as arguments: https://askubuntu.com/questions/1164166/incrond-does-not-execute-custom-script

So your solution may be to first call a wrapper script that does the output redirection to the log file.

0
Roman Mishin On

If you watch a directory, then $@ holds the directory path and $# the file that triggered the event. If you watch a file, then $@ holds the complete path to the file and $# is empty. Reference.