Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inotifywait shell script run as daemon

I have a script that watches a directory (recursively) and performs a command when a file changes. This is working correctly when the monitoring flag is used as below:

#!/bin/sh

inotifywait -m -r /path/to/directory |
    while read path action file; do
            if [ <perform a check> ]
            then
                my_command
            fi
    done

However, I want to run this on startup and in the background, so naïvely thought I could change the -m flag to -d (run inotifywait as daemon, and include an --outfile location) and then add this to rc.local to have this run at startup. Where am I going wrong?

like image 949
Nicholas Avatar asked Jan 16 '18 00:01

Nicholas


2 Answers

Well .... with -d it backgrounds itself and outputs ONLY to outfile, so your whole pipe & loop construct is moot, and it never sees any data.

like image 72
tink Avatar answered Nov 13 '22 15:11

tink


Incron is a cron-like daemon for inotify events.

Just need to use incrontab and an entry for your task:

/path/to/directory IN_ALL_EVENTS /usr/local/bin/my-script $@ $# $%

And /local/bin/my-script would be:

#! /bin/bash
local path=$1
local action=$2
local file=$3
if [ <perform a check> ]
then
  my_command 
fi
like image 3
Gonzalo Matheu Avatar answered Nov 13 '22 15:11

Gonzalo Matheu