0

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.

  • What should happen to files that are dropped during your maintenance period? – roaima Aug 18 '18 at 0:15
  • At 1:15am, and for Linux only, you might be interested by inotify_rm_watch and again at 1:30am, by inotify_add_watch for your specific inotify instance. See man pages for arguments: man7.org/linux/man-pages/man2/inotify_rm_watch.2.html . That may work altough in a rather clunky way. It does not fully address the pointed question asked by @roaima above. – Cbhihe Aug 18 '18 at 8:16
  • @roaima During the maintenance period of 15 minutes, the ftp server is suspended and no files can be dropped. They hold in queue at the sending server until the ftp service is restarted at the end of the maintenance period. – BKM Aug 18 '18 at 14:43
  • After much experimenting, it appears that intoify-tools is not all that good of a solution. In my Ubuntu 16.04 server it is totally dependent on a terminal window being open in order to run. I used a crontab entry to start the script sometime after I logged out. The script ran but with no terminal window session open, the inotifywait command was ignored. So it looks like the original question is invalid as inotifywait cannot run on its own when started blind from a cron triggered script. I really need a solution that can run unattended without requiring me to keep a terminal window open. – BKM Aug 18 '18 at 14:50
  • @BKM I have inotify tools running unattended on a couple of boxes here. – roaima Aug 18 '18 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.