Restart service if executable binary changes

This is a simple bash script to restart a service if the executable binary has been changed, for example during deployment.

#!/bin/bash
SERVICE="{insert service_name}"
FILE="{insert /path/to/service/binary}"
FINGERPRINT="/root/watchers/$SERVICE.md5"

fingerprint_expected=$(/usr/bin/md5sum "$FILE")

if [ ! -e "$FINGERPRINT" ]
then
  echo "$fingerprint_expected" > "$FINGERPRINT"
fi

fingerprint_content=$(/bin/cat "${FINGERPRINT}")

if [ "$fingerprint_expected" != "$fingerprint_content" ];
then
  # Service has new binary
  echo "$fingerprint_expected" > "$FINGERPRINT"
  echo "Service $SERVICE has changed, trying to restart"
  /usr/sbin/service "$SERVICE" restart
fi

This is tested on a Debian system. I call it as a cron job. It is also available on GitHub.

Published: December 30 2014