Run a shell script whenever a file in a certain directory changes?

Using a shell script, can I watch a folder and block program execution until a file in a certain folder changes?

21 points · 22 comments · view on lemmy.world

22 Comments

MyNameIsRichard@lemmy.ml · 13 pts · 1y

There is inotifywait which seems to do the job, The Wiki

helloworld@lemmy.ml · 10 pts · 1y

After some suggestions to check out inotifywait I ended up with a solution that works for me as desired.

inotifywait --event modify,create ./targetfolder/; echo "new change, that I am interested in, occurred in targetfolder";

It turned out I was interested in both file modification and file creation events.

hperrin@lemmy.ca · 9 pts · 1y

As another commenter said, you want inotifywait:

https://linux.die.net/man/1/inotifywait

6nk06@sh.itjust.works · 5 pts · 1y

Maybe inotify or one of those "watcher" (not "watch") tools available, but I don't remember which one to use.

possiblylinux127@lemmy.zip · 2 pts · 1y (2 replies)

What is the end goal?

helloworld@lemmy.ml · 2 pts · 1y (1 reply)

a hobby project: generate a rss feed based on recent file changes in a directory. But I thought this can also have many applications …

  • auto formatting code files on change
  • automatic backups of a certain folder: run rsync on change in folder.
possiblylinux127@lemmy.zip · 4 pts · 1y

Are you familiar with CI/CD pipelines? You could use Git along with a service like Woodpecker CI or Gitlab Runners.

jellyfish@beehaw.org · 1 pts · 1y

The simplest solution is entr, I use it a lot for development - https://www.linuxbash.sh/post/entr-rerun-commands-when-files-change

Shadow@lemmy.ca · 0 pts · 1y

Block execution not entirely. You could chmod it as non-x and use inotifywatch to flip it back.

Edit: I misunderstood you, use inotifywait like the other person suggested.

running_ragged@lemmy.world · -2 pts · 1y (12 replies)

Can continuously loop over the file, examine the md5 hash for changes.

Run the script if it has changed.

https://stackoverflow.com/questions/6475252/bash-script-watch-folder-execute-command

daemon() {
chsum1=""

while [[ true ]]
do
    chsum2=`find src/ -type f -exec md5 {} \;`
    if [[ $chsum1 != $chsum2 ]] ; then           
        if [ -n "$chsum1" ]; then
            compile
        fi
        chsum1=$chsum2
    fi
    sleep 2
done
}
hperrin@lemmy.ca · 15 pts · 1y

Oh god please don’t do this. Constantly reading the file is just stressing your IO for no reason.

Please inotify instead:

https://linux.die.net/man/1/inotifywait

chonkyninja@lemmy.world · 4 pts · 1y

The fuck…

testfactor@lemmy.world · 2 pts · 1y

Even if you wanted to implement a solution like this, which you shouldn't, why on earth monitor the MD5 sum instead of just the mtime of the file???? Like, doing a checksum is the least efficient method of checking this possible.

Like, you could do a simple while loop with a find myfile.txt +mmin 1; sleep 30 in it. Adjust numbers to your desired tolerance.

Again, don't do that. But if you must, definitely don't do an md5sum for godssake.

helloworld@lemmy.ml · -1 pts · 1y (8 replies)

I really like this, replace compile with whatever command you desire I guess.

hperrin@lemmy.ca · 10 pts · 1y (7 replies)

This is a terrible solution. You will stress your IO for no reason.

helloworld@lemmy.ml · 0 pts · 1y (6 replies)

On the upside, you do not need to install the inotifywait package. md5sum already installed on my system haha

hperrin@lemmy.ca · 11 pts · 1y

If you are a big fan of wasting disk performance, CPU cycles, and ultimately power.

possiblylinux127@lemmy.zip · 2 pts · 1y

It isn't a terrible solution if you are checking infrequently just as ever 30 minutes.

nublug@lemmy.blahaj.zone · 2 pts · 1y (3 replies)

why is that a plus

helloworld@lemmy.ml · 1 pts · 1y (2 replies)

I do not need to install anything/can work on bare install without internet connection?

possiblylinux127@lemmy.zip · 1 pts · 1y

You should be able to tie into the kernel with some C programming if you want to go extra small.

nublug@lemmy.blahaj.zone · 1 pts · 1y

ah, fair.