Run Systemd Service On Matching Journal Lines
In this guide, we will walk through setting up a Systemd service that runs based on matching journal lines. This technique is particularly useful for automating tasks in self-hosted infrastructure.
# Run Systemd Service On Matching Journal Lines
In this guide, we will walk through setting up a Systemd service that runs based on matching journal lines. This technique is particularly useful for automating tasks in self-hosted infrastructure and homelabs, enhancing DevOps workflows by implementing powerful automation.
Prerequisites
To follow this tutorial, ensure you have the following prerequisites:
A system running Linux with Systemd 240 or later installed. You can check your version using
systemctl --version
.Familiarity with basic shell scripting and Systemd service management.
Step-by-step Guide
1. Create a Custom Journal Match File
Create a new file under /etc/systemd/journald.conf.d/
with a .conf
extension, for example: custom_match.conf
. In this file, define your custom journal match rules to trigger the service.
1
2
3
4
# /etc/systemd/journald.conf.d/custom_match.conf
[Journal]
MatchPath=kernel:/path/to/your/log/*.log
MatchLogLevel=warning,error
Replace /path/to/your/log/
with the path to your log file, and adjust the MatchLogLevel
directive as needed for your use case.
2. Create a Systemd Service File
Create a new Systemd service file in /etc/systemd/system/
, for example: my_service.service
. In this file, define your custom service and its actions based on the journal match rules.
1
2
3
4
5
6
7
8
9
10
11
12
# /etc/systemd/system/my_service.service
[Unit]
Description=My Service that runs on matching journal lines
Documentation=https://wiki.debian.org/Systemd
[Service]
Type=oneshot
ExecStart=/path/to/your/script.sh restart
Restart=always
[Install]
WantedBy=multi-user.target
Replace /path/to/your/script.sh
with the path to your custom script that performs the desired action when triggered. The restart
option ensures the service will be restarted if it fails, providing more robustness.
3. Enable and Start the Service
Now that you have defined your custom Systemd service, enable and start it using the following commands:
1
2
3
sudo systemctl daemon-reload
sudo systemctl enable my_service.service
sudo systemctl start my_service.service
4. Test Your Setup
To test your setup, generate an error or warning in the log file that matches your defined journal match rules and verify that the service is executed accordingly.
Troubleshooting
If you encounter issues, check the system logs for any relevant errors, and ensure that your journal match rules are correctly configured and matching the desired log lines. Additionally, confirm that your script is functioning as expected when run manually.
Conclusion
In this guide, we demonstrated how to create a custom Systemd service that runs based on matching journal lines. By utilizing this technique, you can enhance automation in self-hosted infrastructure and homelabs for efficient DevOps workflows using open-source tools.
Note: Be aware of potential security considerations when implementing this setup, such as ensuring proper permissions and access control for your log files and custom scripts. Additionally, consider performance optimization by monitoring resource usage and fine-tuning your journal match rules and scripts accordingly.