WHAT IS THE PROBLEM WHEN USING kill <pid>?
You might have more than one inotifywait processes because other scripts can use inotifywait independently. So using this command ps -ef | grep inotifywait to find the right PID is not the best thing to do because you need to have a good assumption which inotifywait process belongs to your script. So, you might end up killing the wrong PID. Besides, the command killall inotifywait is more aggressive than the previous one. However, if you really don't care other system is using inotifywait, you use the aggressive command.
MY BEST WAY TO KILL inotifywait PROCESS FOR EACH inotifywait instance
You can create a file flag to terminate inotifywait for a specific running script. Below scripts is example how you start the inotify script, stop it, or even test if it's running for that specific inotify PID.
#!/bin/bash
# inotify-test.sh
test="changes.txt" # Monitoring file
flag="flag.txt" # For stopping, starting event or whatever flag you need
REPORT_FILE="report.txt"
cat /dev/null > $REPORT_FILE
pid="inotify-test.pid"
cat /dev/null > $max_pid
ARGS1="$1"
function start_monitor {
If file not exist, then write default start
if [ -f "${flag}" ]; then
echo "start" > "${flag}"
fi
Do not use -q -q (twice), because it will not output anything after do while loop
(echo "$BASHPID" > $pid; exec inotifywait -q $flag $test -e modify -m) |
while read file action
do
process_flag=$(head -1 ${flag})
if [ "${process_flag}" == "stop" ]; then
echo "Process stopped" >> $REPORT_FILE
kill -- -$$
elif [ "${process_flag}" == "sleep" ]; then
echo "Process sleep: $$" >> $REPORT_FILE
sleep 5
# Important, after sleep, must write start to start
echo "start" > "${flag}"
elif [ "${process_flag}" == "start" ]; then
echo "Process is running" >> $REPORT_FILE
# Only run here if flag is start
echo "File: $file, action: $action, date date" >> $REPORT_FILE
else
echo "Invalid flag $$" >> $REPORT_FILE
fi
done & # This symbol & is important, it will run this inotify in background
}
if [ "${ARGS1}" == "start" ]; then
start_monitor
fi
if [ "${ARGS1}" == "stop" ]; then
echo "stop" > "${flag}"
fi
if [ "${ARGS1}" == "test" ]; then
echo "sleep" > "${flag}"
fi
You can watch this action from the log file $REPORT_FILE
tail -f report.txt
To start monitoring, you can use:
./inotify-test.sh start
To test the script with if inotifywait is running you do this command:
./inotify-test.sh test
So, to stop the inotify running process, you just need to run the same script with
./inotify-test.sh stop
With this method, you don't need to know what is the process ID for that inotifywait process.
HOW DOES IT WORK?
You notice that I have 2 files to monitor $flag $test from the inotifywait command so if I make changes to the $flag file, the modify even will be triggered immediately and I can use this opportunity to stop the PID process inside the inotifywait loop. Also, you can see that the script actually stores an actual pid of inotifywait at pid="inotify-test.pid". So you can manually terminate the inotifywait process using this correct pid.