Auto-refreshing Google Chrome on file changes

I am known for being an lazy ass, one of the ways of coping with this horrible disease is automating any repetitive task that takes more that 5 minutes of work. So, in my free time, when I’m not dreaming of eating cake on a magical dessert island, I have the habit of bashing away at my keyboard to write shell scripts that ease my life.

During the process of writing a HTML presentation for my school and while trying to find ways to procrastinate and avoid it, I head this little voice in my head: “Hey, psstt, you, wouldn’t it be neat if you didn’t have to spend those 5 seconds refreshing the browser window each time you save that file?”.

After consulting my trusty “Is it work the time chart?” and seeing that I have lots of free time, with the weekend ahead of me, I’ve decided to listen to my little procrastination demon and dive in to see how I can shave those 5 seconds off.

It turns out it’s pretty easy to do, there are two nice system utilities on Linux that you can easily glue together by writing a bash script.

inotifywait inotifywait is a command-line utility that uses the inotify Linux kernel API which provides a mechanism for monitoring filesystem events. The API can be used to monitor individual files, or to monitor directories. When a directory is monitored, inotify will return events for the directory itself, and for files inside the directory. Events are fired, for example, for file and directory creation, deletion, access, modification, etc (for a full list, read the manual). inotifywait, as the name suggests, will listen and wait for an event returning a code if the events for which we are listening to are triggered.

xdotool xdotool is a command-line utility that lets you automate keyboard input and mouse activity, move and resize windows, etc. It basically lets you control your GUI application. TuxRadar has a really nice tutorial about it if you want to find out more.