Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 68 lines (58 sloc) 2.38 KB
#!/bin/sh
#
# Autosave configuration daemon for BSD Router Project
# http://bsdrp.net
#
# Copyright (c) 2017, The BSDRP Development Team
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
set -eu
# Global variables
lock="/var/run/autosave.bsdrp.lock"
# A usefull function (from: http://code.google.com/p/sh-die/)
die() { echo -n "EXIT: " >&2; echo "$@" >&2; exit 1; }
# Because we are using running script, we need to cleanup mess before leaving
cleanup () {
logger autosave stoping
pkill fswatch || true
[ -d $lock ] && rm -rf $lock
}
logger autosave starting
# Prevent to execute this script twice
trap cleanup EXIT INT TERM
# We need a method for checking AND creating lock file in same step
if ! mkdir $lock 2>/dev/null; then
logger "autosave is already running (file $lock detected)"
echo "This script is already running (file $lock detected)"
usage
fi
[ -x /usr/local/bin/fswatch ] || die "fswatch is mandatory"
echo "Watching /etc for change..."
# Need to exit fswatch once change detected for avoid multiple triggers
while true; do
if fswatch -1dr /etc; then
logger autosave triggered
config save || logger autosave failed to save configuration
fi
done