Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ _lint-shell:
@echo "# -------------------------------------------------------------------- #"
@echo "# Shellcheck"
@echo "# -------------------------------------------------------------------- #"
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt -w /mnt koalaman/shellcheck:stable watcherd
@docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt -w /mnt koalaman/shellcheck:stable bin/watcherd


# -------------------------------------------------------------------------------------------------
# Test Targets
# -------------------------------------------------------------------------------------------------
test:
./test/01.sh
./tests/01.sh
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# watcherd

![tag](https://img.shields.io/github/v/tag/devilbox/watcherd.svg?colorB=orange&sort=semver)
[![linting](https://github.com/devilbox/watcherd/workflows/linting/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Alinting)
[![test-linux](https://github.com/devilbox/watcherd/workflows/test-linux/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Atest-linux)
[![test-macos](https://github.com/devilbox/watcherd/workflows/test-macos/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Atest-macos)
[![test-windows](https://github.com/devilbox/watcherd/workflows/test-windows/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Atest-windows)
[![linting](https://github.com/devilbox/watcherd/workflows/linting/badge.svg)](https://github.com/devilbox/watcherd/actions/workflows/linting.yml)
[![test-linux](https://github.com/devilbox/watcherd/workflows/test-linux/badge.svg)](https://github.com/devilbox/watcherd/actions/workflows/test-linux.yml)
[![test-macos](https://github.com/devilbox/watcherd/workflows/test-macos/badge.svg)](https://github.com/devilbox/watcherd/actions/workflows/test-macos.yml)
[![test-windows](https://github.com/devilbox/watcherd/workflows/test-windows/badge.svg)](https://github.com/devilbox/watcherd/actions/workflows/test-windows.yml)
[![License](https://img.shields.io/badge/license-MIT-%233DA639.svg)](https://opensource.org/licenses/MIT)

**[watcherd](https://github.com/devilbox/watcherp/blob/master/watcherd)** will look for directory changes (added and deleted directories) under the specified path (`-p`) and will execute specified commands or shell scripts (`-a`, `-d`) depending on the event.

**[watcherd](https://github.com/devilbox/watcherd/blob/master/bin/watcherd)** will look for directory changes (added and deleted directories) under the specified path (`-p`) and will execute specified commands or shell scripts (`-a`, `-d`) depending on the event.
Once all events have happened during one round (`-i`), a trigger command can be executed (`-t`).
Note, the trigger command will only be execute when at least one add or delete command has succeeded with exit code 0.

Expand All @@ -18,7 +20,7 @@ If you need the same functionality to monitor changes of listening ports, check

### Modes

**[watcherd](https://github.com/devilbox/watcherp/blob/master/watcherd)** can either use the native [inotifywait](https://linux.die.net/man/1/inotifywait) implementation or if this is not available on your system use a custom bash implementation. The default is to use bash.
**[watcherd](https://github.com/devilbox/watcherd/blob/master/bin/watcherd)** can either use the native [inotifywait](https://linux.die.net/man/1/inotifywait) implementation or if this is not available on your system use a custom bash implementation. The default is to use bash.

### Placeholders

Expand All @@ -31,14 +33,14 @@ You can specify the placeholders as many times as you want. See the following ex

### Examples

By using **[vhost_gen.py](https://github.com/devilbox/vhost-gen)** (which is capable of creating Nginx or Apache vhost config files for normal vhosts or reverse proxies), the following will be able to create new nginx vhosts on-the-fly, simply by adding or deleting folders in your main www directory. The trigger command will simply force nginx to reload its configuration after directory changes occured.
By using **[vhost-gen](https://github.com/devilbox/vhost-gen)** (which is capable of creating Nginx or Apache vhost config files for normal vhosts or reverse proxies), the following will be able to create new nginx vhosts on-the-fly, simply by adding or deleting folders in your main www directory. The trigger command will simply force nginx to reload its configuration after directory changes occured.

```shell
# %n will be replaced by watcherd with the new directory name
# %p will be replaced by watcherd with the new directory path
watcherd -v \
-p /shared/httpd \
-a "vhost_gen.py -p %p -n %n -s" \
-a "vhost-gen -p %p -n %n -s" \
-d "rm /etc/nginx/conf.d/%n.conf" \
-t "nginx -s reload"
```
Expand Down Expand Up @@ -86,3 +88,9 @@ Misc arguments:
--help Show this help screen.
--version Show version information.
```

### License

**[MIT License](LICENSE)**

Copyright (c) 2017 [cytopia](https://github.com/cytopia)
49 changes: 36 additions & 13 deletions watcherd → bin/watcherd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MY_DATE="2022-12-17"
MY_URL="https://github.com/devilbox/watcherd"
MY_AUTHOR="cytopia <cytopia@everythingcli.org>"
MY_GPGKEY="0xA02C56F0"
MY_VERSION="1.0.5"
MY_VERSION="1.0.6"
MY_LICENSE="MIT"

# Default settings
Expand Down Expand Up @@ -174,54 +174,78 @@ while [ $# -gt 0 ]; do
case "${1}" in
-p)
shift
if [ -z "${1:-}" ]; then
>&2 echo "${MY_NAME}: -p requires an argument."
exit 1
fi
if [ ! -d "${1}" ]; then
>&2 echo "Specified directory with -p does not exist: '${1}'."
>&2 echo "${MY_NAME}: Specified directory with -p does not exist: '${1}'."
exit 1
fi
WATCH_DIR="${1}"
;;
-a)
shift
if [ -z "${1:-}" ]; then
>&2 echo "${MY_NAME}: -a requires an argument."
exit 1
fi
if [ "${1:0:1}" = "-" ]; then
>&2 echo "Specified add command cannot start with '-': '${1}'."
>&2 echo "${MY_NAME}: Specified add command cannot start with '-': '${1}'."
exit 1
fi
CMD_ADD="${1}"
;;
-d)
shift
if [ -z "${1:-}" ]; then
>&2 echo "${MY_NAME}: -d requires an argument."
exit 1
fi
if [ "${1:0:1}" = "-" ]; then
>&2 echo "Specified del command cannot start with '-': '${1}'."
>&2 echo "${MY_NAME}: Specified del command cannot start with '-': '${1}'."
exit 1
fi
CMD_DEL="${1}"
;;
-t)
shift
if [ -z "${1:-}" ]; then
>&2 echo "${MY_NAME}: -t requires an argument."
exit 1
fi
if [ "${1:0:1}" = "-" ]; then
>&2 echo "Specified trigger command cannot start with '-': '${1}'."
>&2 echo "${MY_NAME}: Specified trigger command cannot start with '-': '${1}'."
exit 1
fi
CMD_TRIGGER="${1}"
;;
-w)
shift
if [ -z "${1:-}" ]; then
>&2 echo "${MY_NAME}: -w requires an argument."
exit 1
fi
if [ "${1}" != "bash" ] && [ "${1}" != "inotify" ]; then
>&2 echo "Specified watcher with -w must either be 'bash; or 'inotify': '${1}'."
>&2 echo "${MY_NAME}: Specified watcher with -w must either be 'bash; or 'inotify': '${1}'."
exit
fi
if [ "${1}" = "inotify" ]; then
if ! command -v inotifywait >/dev/null 2>&1; then
>&2 echo "Specified watcher 'inotify' requires 'inotifywait' binary. Not found."
>&2 echo "${MY_NAME}: Specified watcher 'inotify' requires 'inotifywait' binary. Not found."
exit
fi
fi
WATCHER="${1}"
;;
-i)
shift
if [ -z "${1:-}" ]; then
>&2 echo "${MY_NAME}: -i requires an argument."
exit 1
fi
if ! echo "${1}" | grep -Eq '^[1-9][0-9]*$'; then
>&2 echo "Specified interval with -i is not a valid integer > 0: '${1}'."
>&2 echo "${MY_NAME}: Specified interval with -i is not a valid integer > 0: '${1}'."
exit 1
fi
INTERVAL="${1}"
Expand All @@ -238,8 +262,7 @@ while [ $# -gt 0 ]; do
exit 0
;;
*)
echo "Invalid argument: ${1}"
echo "Type '${MY_NAME} --help' for available options."
echo "${MY_NAME}: Invalid argument: ${1}. Type --help for available options."
exit 1
;;
esac
Expand All @@ -248,15 +271,15 @@ done

# Make sure required arguments are set
if [ -z "${WATCH_DIR}" ]; then
>&2 echo "Error: -p is required. Type --help for more information."
>&2 echo "${MY_NAME}: Error: -p is required. Type --help for more information."
exit 1
fi
if [ -z "${CMD_ADD}" ]; then
>&2 echo "Error: -a is required. Type --help for more information."
>&2 echo "${MY_NAME}: Error: -a is required. Type --help for more information."
exit 1
fi
if [ -z "${CMD_DEL}" ]; then
>&2 echo "Error: -d is required. Type --help for more information."
>&2 echo "${MY_NAME}: Error: -d is required. Type --help for more information."
exit 1
fi

Expand Down
2 changes: 1 addition & 1 deletion test/01.sh → tests/01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -o pipefail

SCRIPT_PATH="$( cd "$(dirname "$0")" && pwd -P )"

BIN_PATH="${SCRIPT_PATH}/.."
BIN_PATH="${SCRIPT_PATH}/../bin"
DIR_PATH="${SCRIPT_PATH}/dirs"


Expand Down