run systemd service on matching journal lines
Posted by swb0z0@reddit | linuxadmin | View on Reddit | 4 comments
What would be the easiest/best way to trigger a systemd
one-shot service when a systemd
journal line matches a given pattern?
I've tried cobbling together a shell script using journalctl -f -u SERVICE | grep PATTERN
running as a separate service instance, but the triggering is delayed, possibly due to stdio buffering.
The use case I'm attempting to address is a simple form of service monitoring; perhaps there's an existing open-source software package that already accommodates this.
The_Real_Grand_Nagus@reddit
I have not done this personally, but Consider using tools like journal-trigger, which can monitor journal entries and execute actions based on defined rules. This tool allows for more granular control and can be configured to trigger services when specific conditions are met in the journal entries.
chock-a-block@reddit
https://www.freedesktop.org/software/systemd/man/latest/systemd-journal-gatewayd.service.html
There was a decision at some point to deprecate syslog support in systemd that makes your job harder.
vogelke@reddit
You may be right about the buffering. If you are using GNU Grep, use grep --line-buffered
instead. For programs that don't have dedicated options for this, use stdbuf: stdbuf -oL journalctl -f -u SERVICE | grep PATTERN
sudonem@reddit
You need to write a shell script (or python script) that is constantly monitoring the logs and calls the one-shot service when your regex pattern is detected.