watch a log file and run the script matching .well-known/wakeup-on-logs/myscript
This repository has been archived on 2024-11-12. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
Find a file
Earl Warren 340343c5e1
All checks were successful
/ test (push) Successful in 20s
document throttling reset re-running the last task
2024-07-01 11:51:43 +02:00
.forgejo/workflows shfmt 2024-07-01 10:17:22 +02:00
.editorconfig shfmt 2024-07-01 10:17:22 +02:00
.gitignore initial 2024-06-30 08:32:18 +02:00
LICENSE Initial commit 2024-06-30 05:31:18 +00:00
README.md document throttling reset re-running the last task 2024-07-01 11:51:43 +02:00
wakeup-on-logs-test.sh shfmt 2024-07-01 10:17:22 +02:00
wakeup-on-logs.sh tasks run as root 2024-07-01 11:23:56 +02:00

wakeup-on-logs.sh

Runs tail -f /var/log/nginx/access.log looking for the .well-known/wakeup-on-logs/([a-zA-Z0-9_.-]+) pattern. When a match is found, the matching script found in /etc/wakeup-on-logs is run. For instance, /etc/wakeup-on-logs/myscript will run if .well-known/wakeup-on-logs/myscript is found in the logs.

When a script is added to /etc/wakeup-on-logs, the daemon must be notified with sudo systemctl restart wakeup-on-logs-watch.

How does it work?

The systemctl status wakeup-on-logs-watch daemon tail -f /var/log/nginx/access.log. If the .well-known/wakeup-on-logs/([a-zA-Z0-9_.-]+) pattern is found, it will touch the corresponding file in /var/lib/wakeup-on-logs, if it exists. It runs under an unprivileged user and will discard a pattern that occur more frequently than every five minutes.

The systemctl status wakeup-on-logs-run daemon runs as root and waits for file modifications in /var/lib/wakeup-on-logs. When it finds one file was modified, it runs the matching script found in /etc/wakeup-on-logs. It runs one script at a time and imposes a five minutes timeout for them to complete.

Install and upgrades

Requires a running nginx server.

sudo wget -O /usr/local/bin/wakeup-on-logs.sh https://code.forgejo.org/infrastructure/wakeup-on-logs/raw/branch/main/wakeup-on-logs.sh ; sudo chmod +x /usr/local/bin/wakeup-on-logs.sh
sudo wakeup-on-logs.sh install

Reset throttling

sudo systemctl restart wakeup-on-logs-watch

It will also read the last 10 lines (default of tail(1)) of the log. If they contain a pattern for a task, it will run it.

Trigger a task

sudo journalctl --follow --unit wakeup-on-logs-run --unit wakeup-on-logs-watch
curl -o /dev/null -sS http://0.0.0.0/.well-known/wakeup-on-logs/hello

Add a task to the watch list

( echo '#/bin/bash' ; echo echo hello ) | sudo tee /etc/wakeup-on-logs/hello ; sudo chmod +x /etc/wakeup-on-logs/hello
sudo systemctl restart wakeup-on-logs-watch
test $(curl -o /dev/null -w "%{http_code}" -sS http://0.0.0.0/.well-known/wakeup-on-logs/echo) = 404 && echo OK
sudo systemctl status wakeup-on-logs-run

Remove a task from the watch list

rm /etc/wakeup-on-logs/hello
sudo systemctl restart wakeup-on-logs-watch

Update a task already in the watch list

$EDITOR /etc/wakeup-on-logs/hello