Incron Replacement with Systemd
In recent debian release «bullsey» incron, the file system watcher using inotify, has been depricated and removed from the pacakges. Reason for this is, that the package is unmaintained. While looking for alternatives I stumbled over a solution with systemd.
Goal was it to observe changes in a config file and act when it was closed.
I am using hugo for my static website generation and when i set deploy = true in the config
file of the test site, I want the system to automatically build the site
and deploy it on to the public server.
Systemd
Systemd needs 2 units to make this work. One configuration
file is a path unit called hugo-deploy.path which configures the observation part of the file
and the other is the service for acting upon changes hugo-deploy.service.
hugo-deploy.path
1# https://forum.howtoforge.com/threads/replacing-incron-with-systemd.88477/
2
3[Unit]
4Description="Monitor config.toml. when deploy= changes, run the deploy script""
5
6[Path]
7# file to observe
8PathChanged=/path/to/dev/root/config.toml
9# optional when different file names are used
10#Unit=hugo-deploy.service
11
12[Install]
13WantedBy=multi-user.target
hugo-deploy.service
1[Unit]
2Description="Monitor hugo config file"
3
4[Service]
5type=simple
6ExecStart=/path/to/web/root/bin/hugo-deploy.sh
7User=wus
8Group=nogroup
9
10[Install]
11WantedBy=multi-user.target
Hugo Deploy scripts
hugo-deploy.sh
1#!/usr/bin/env bash
2
3script_dir=$(readlink -f $(dirname $0))
4cd "$script_dir/.."
5
6# grep for "^deploy.*" line
7# extract value after =
8# convert to lower case
9value=$(grep "^deploy[[:space:]]*=" /path/to/web/root/config.toml | \
10 sed -e 's,.*=[[:space:]]*,,' | tr '[:upper:]' '[:lower:]')
11
12# log
13echo "$(date) $USER: $value" >> /path/to/web/root/var/hugo-deploy.service.log
14
15# if true, deploy hugo site
16if [[ "$value" = "true" ]]; then
17 # deploy site
18 "$script_dir/../bin/deploy.sh"
19
20 # revert config file
21 sed -i 's,^deploy[[:space:]]*=.*$,deploy = false,' \
22 /path/to/web/root/config.toml >> \
23 /path/to/web/root/var/hugo-deploy.service.log
24else
25 echo "not true" >>/path/to/web/root/var/hugo-deploy.service.log
26fi
27
28exit 0
deploy.sh
This bash script is used for creating a build and deploying it to the live server
1#!/usr/bin/env bash
2
3script_dir=$(readlink -f $(dirname $0))
4cd "$script_dir/.."
5
6# list uncommited changes
7changes=$(git status | grep content/)
8if [[ ! -z "$changes" ]]; then
9 echo "==> Uncommited changes"
10 echo "$changes"
11 echo "________________________________________________________________________________"
12 echo ""
13fi
14
15hugo --destination=/path/to/web/root/www \
16 --cleanDestinationDir \
17 --logFile /path/to/web/root/var/deploy.log \
18 --baseURL "https://wunderlin.net/" \
19 --minify \
20 --forceSyncStatic \
21 --gc