Wednesday, May 30, 2012

inotifywait - run scripts on file changes

When working on a project in Java, I often need to stop/start maven whenever I make changes to the source code. I wanted to have maven restart automatically whenever I changed the source code. I ended up using some bash scripts to get the job done. inotifywait will run the script restart.sh whenever a file inside src is saved. This could also be done to automatically run test when source code changes. watch.sh:
#!/bin/bash
while inotifywait -re close_write src; do ./restart.sh; done
restart.sh:
#!/bin/bash
./kill_tomcat.sh
sleep 1
mvn tomcat:run -o &
kill_tomcat.sh:
#!/bin/bash
pkill -kill -f tomcat:run