Nicholas
Nicholas

Reputation: 576

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?

Upvotes: 6

Views: 7679

Answers (3)

Jebby
Jebby

Reputation: 1955

You need to add a single & to the end of command in your /etc/rc.local

Putting a single & at the end of a command means Run this program in the background so the user can still have input.

Upvotes: 3

Gonzalo Matheu
Gonzalo Matheu

Reputation: 10104

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

Upvotes: 5

tink
tink

Reputation: 15238

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.

Upvotes: 4

Related Questions