Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart process on file change in Linux

Tags:

linux

shell

Is there a simple solution (using common shell utils, via a util provided by most distributions, or some simple python/... script) to restart a process when some files change?

It would be nice to simply call sth like watch -cmd "./the_process -arg" deps/*.

Update: A simple shell script together with the proposed inotify-tools (nice!) fit my needs (works for commands w/o arguments):

#!/bin/sh while true; do   $@ &   PID=$!   inotifywait $1   kill $PID done 
like image 461
Christian Avatar asked Sep 04 '12 13:09

Christian


1 Answers

Yes, you can watch a directory via the inotify system using inotifywait or inotifywatch from the inotify-tools.

inotifywait will exit upon detecting an event. Pass option -r to watch directories recursively. Example: inotifywait -r mydirectory.

You can also specify the event to watch for instead of watching all events. To wait only for file or directory content changes use option -e modify.

like image 107
scai Avatar answered Sep 18 '22 17:09

scai