Process to read a new file entry and execute a command

I need to develop a process/daemon which will constantly monitor a file for new entry and execute a command.

for eg, there is a file /var/log/inotify.log

When a new entry like below gets appeneded to this file, execute the command as follows.

/home/user/public_html/bad.php|CREATE

Command to execute

clamdscan --log=/var/log/clamav.log --move=/usr/src/clamav_quarantine $var

$var should be /home/user/public_html/bad.php

idea is to scan all newly created files using clamscan

have a program for the following?

LOOP1:
store the current result of a wc-l command (to count lines)
wait x time (sleep)
store a new value for wc-l
if two vars not equal, then execute your command
goto LOOP1

There are other approaches also.

Code for @joeyg

logFile=$1

while [ 1 ]
do
	curr=`wc -l $logFile`
	if [ "$curr" != "$prev" ]
	then
		echo "Command to execute"
	fi
	prev=$curr
	sleep 1

done

If you are looking for real process with realtime signaling instead of polling, you can use inotify() system call with IN_MODIFY.

1 Like