Search code examples
linuxshellshinotifyinotifywait

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?


Solution

  • 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.