Skip to content

gitwatch as a service on FreeBSD

falkenbe edited this page Nov 24, 2020 · 4 revisions

This script will start gitwatch using the rc system and has been tested on FreeBSD 12.1.

Install prerequisites

bash, git, and inotify-tools are required by gitwatch

sudo pkg install bash git inotify-tools

Install gitwatch

I installed gitwatch in /opt/gitwatch/bin to keep it separate from programs installed with ports/packages in /usr/local

sudo mkdir -p /opt/gitwatch/bin
# Install from source instructions from gitwatch repo
cd /tmp
git clone https://github.com/gitwatch/gitwatch.git
cd gitwatch
sudo install -b gitwatch.sh /opt/gitwatch/bin/
#
sudo chown -R root:wheel /opt/gitwatch
sudo chmod -R go=u-w /opt/gitwatch

Create git repositories in gitwatch target directories

Each target directory monitored by gitwatch needs a git repository. If there isn't an existing repository, create one by running git init in each target directory.

cd /etc
sudo git init
sudo git add .
sudo git commit -m 'Initial Creation'
#
cd /usr/local/etc
sudo git init
sudo git add .
sudo git commit -m 'Initial Creation'

Create gitwatch rc script

Create the following script named gitwatch in /usr/local/etc/rc.d/. The command and PATH variables may need to be adjusted if gitwatch.sh, bash, git, or inotifywait are installed in other directories.

#! /bin/sh
#
# Custom script for gitwatch
# falkenbe - 2020-08-02
#

# PROVIDE: gitwatch
# REQUIRE: DAEMON

#
# Add the following line to /etc/rc.conf to enable gitwatch:
#
#gitwatch_enable="YES"
#
# To set the directories gitwatch watches add the following line to
# /etc/rc.conf.  Multiple targets can be separated by spaces
#
#gitwatch_targets="/etc /usr/local/etc /some/other/directory"
#

. /etc/rc.subr

name="gitwatch"
rcvar="gitwatch_enable"

start_cmd="${name}_start"

load_rc_config $name

: ${gitwatch_enable:=NO}
: ${gitwatch_targets:="/etc /usr/local/etc"}
: ${gitwatch_args:="-l 10"}

command="/opt/gitwatch/bin/gitwatch.sh"
command_interpreter='bash'
command_args="${gitwatch_args}"

# bash, git, and inotifywait must be on the PATH for gitwatch to work.
# all are in /usr/local/bin if installed from ports/packages.
PATH="${PATH}:/usr/local/bin"

gitwatch_start() {

    # Start a gitwatch for each target
    for TARGET in ${gitwatch_targets}; do
            ${command} ${command_args} "${TARGET}" >/dev/null &
    done
}

run_rc_command "$1"

Edit rc.conf

Add the following line to /etc/rc.conf

gitwatch_enable="YES"

The gitwatch rc script will monitor /etc and /usr/local/etc by default. To change which directories are watched, tune the gitwatch_targets variable in /etc/rc.conf:

gitwatch_targets="/etc /usr/local/etc /some/other/directory"

Using rc script

  • Start: sudo service gitwatch start
  • Stop: sudo service gitwatch stop
  • Restart: sudo service gitwatch restart
  • Get current status: service gitwatch status