I have a script that starts up a perpetual inotifywait session (using the -m switch) that monitors a directory waiting for backup files to drop in and then moves them to another directory where they can be managed. That script looks like this:
dir=/home/userid/drop/ target=/home/userid/current/ inotifywait -m "$dir" --format '%w%f' -e create | while read file; do mv "$file" "$target" find /home/userid/current -maxdepth 1 -type f -name "*.sql.gz" -print0 | xargs -r0 ls -t | tail -n +5 | tr '\n' '\0' | xargs -r0 rm done
The above script moves the files as soon as they drop and then purges the target directory down to the last 4 files. This works nicely and it can be set to start from a crontab entry likely around 1:30am.
However, 23 hours and 45 minutes later, I then need to be able to shut down the perpetual inotifywait process and perform some other maintenance before allowing it to restart again. So I would like to be able to have another crontab entry that calls another script to shut down the running inotifywait process, performs the other maintenance and then exits. This would then allow the first crontab entry to restart the directory monitoring.
I need this to all run unattended without having to be logged into a terminal session. Is this possible? If so, what command would I use in the maintenance script to stop the inotifywait session started by the other script?
Any guidance will be greatly appreciated.