I am new to scripting, I made a script that reads 2 files and execute commands and as output a log file.

I want this script to run like a daemon and wait for changes in these 2 files to execute these commands again on theses files.

I am aware of using some inotify, but I don't know exactly how to use it and where to place the loop so the script will work as I want it too, that is:

  1. user executes the script for the first time
  2. this script reads these files and make the output log
  3. the script "sleeps" (daemonized) until these 2 log files change (some lines added to them)
  4. execute the same script again on these files
  5. sleep again and wait for changes in these 2 files

3 Answers 3


I had to monitor an old software, which does not have enough logging options. It is not exactly what you want, but might help you.

  • compares 2 files (ref.cfg and cur.cfg) every second
  • logs to file cfg.log if a diff is detected
  • makes a diff to a file if a change is detected
  • makes a backup of the file with date suffix




while true; 
  cfg1=$(cat $cfg_reference)
  cfg2=$(cat $cfg_current)
  date_current="$(date +"%F_%T")"
  test "$cfg1" = "$cfg2" || diff_detected=true
  if [ "$diff_detected" = true ]; then
    printf "$date_current [cfg] diff detected\n" >> cfg.log
    diff $cfg_reference $ecfg_current > "./cfg_diff/cur.cfg_${date_current}"
    cp -a "$cfg_reference" "./cfg_old/cur.cfg_before_${date_current}"
    cp -a "$cfg_current" "$cfg_reference"
  sleep 1

Run those commands:

chmod +x watch_cfg.sh
nohup ./watch_cfg.sh  > /dev/null 2>&1 &

This detaches the process from the current session and sends it to the background.


I thought this might help you out so I'll post it as an "answer" (if I could just comment I would do that).


Example 4 might be in the area of what you're trying to do. I've used it in the distant past with good results.

./fileschanged -r dir | while read file ; do
  if [ "${file}" == "x" ] ; then
    handle_file_x_script "${file}"
  else if [ "${file}" == "y" ] ; then
    handle_file_y_script "${file}"
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Archemar
    Dec 20, 2016 at 13:39
  • yeah, if i could COMMENT on the post i would have just posted it as a comment. but i can't, so rather than not help the OP i put it here anyway. I'm not saying that's an answer. Dec 20, 2016 at 13:44
  • You could simply copy/paste and edit sample line, that would be a answer.
    – Archemar
    Dec 20, 2016 at 13:50

You could use incron to listen to inotify events. Packages are availables in debian-like (ubuntu,…) distribution and probably in others as well

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.