

Streamlining MySQL Task Scheduling with Systemd and Percona Tools
Systemd offers a powerful alternative to cron for managing and scheduling MySQL administration tasks. This blog post explores how to leverage systemd services to integrate various Percona Toolkit utilities like pt-heartbeat and pt-kill for optimal MySQL performance and health.
In my day to day tasks as a DB Engineer at Mydbops we have been using multiple MySQL tools for multiple use cases to ensure an optimal performance and availability for servers managed by our Remote DBA Team.
A tool like pt-online-schema can be used for any DDL changes ( Overview to DDL algorithm ), if any tool which needs to scheduled for longer period we tend to use screen or cron.
Some of the problems we face when we demonise the process or use screen for running processes.
- The daemon process gets killed when the server reboot happens.
- The screen might accidentally terminate while closing it.
- To flexibility to start or stop the process when required.
These common problem can be overcome by using systemd service.
What is Systemd?
Systemd is a system and service manager for Linux operating systems. When running as the first process on boot (as PID 1), it acts as an init system that brings up and maintains userspace services.
List of few use cases that can be made simple with systemd.service.
- Integrating Pt-heartbeat with Systemd Service
- Integrating Auto kill using pt-kill with Systemd Service.
- Integrating multi query killer with Systemd service
Integrating Pt-heartbeat with Systemd Service
We had the requirement to schedule pt-heartbeat to monitor replication lag for one of our clients under our database managed Services. Here is problem statement pt-heartbeat process was running as a daemon process, the usual problem we were facing was when the system is rebooted for any maintenance , the pt-heartbeat process gets killed and we start receiving the replication lag alerts and then it needs a manual fix.
Script for pt-heartbeat
Now let us integrate it with systemd
ExecStart = It needs the command which needs to be executed when the service kick start )
Restart = Always option specifies to start the process once the OS is booted up.
Once the new systemd script is pushed, Reload the systemctl daemon and start the service
This service can be stopped by just giving ( similar to to any systemctl process )
Integrating Auto kill using pt-kill with Systemd Service
Usually in production servers long running queries will spike the system resource usage and degrade the MySQL performance drastically or might kill your MySQL process with OOM killer, in order to avoid this hiccups , we can schedule Percona pt-kill process based on the use case defined.
Scheduling the pt-kill service
Now we have configured a fail safe pt-kill process.
Integrating multi query killer with Systemd service
Question : Is it possible to integrate multiple Kill Statements for different hosts as single process.
Answer – Yes ! It is possible and quite simple too.
Just add the needed commands as shell script file and make it execute it. In the below example i have chose three different server consider a RDS instance ( more on AWS RDS its Myth ) and a couple of virtual machine.
Scheduling pt-kill.service for multiple hosts
Reload the daemon and start the service
This makes Systemd more useful and easy tool for scheduling mysql tools in database environment. There are many more features in Systemd that be used for scheduling scripts bypassing the use of crontab, hopefully.
Note : All these are sample scripts you ensure you test well before making it in production.
Mydbops offers a comprehensive suite of open-source database management services specifically designed for MySQL.
{{cta}}
FAQs
What is systemd and how is it used with MySQL?
systemd is the default init system on most modern Linux distributions. It manages services during boot and runtime. When integrated with MySQL tools, systemd can automate database start, stop, restart, and health-check operations, improving service reliability.
Why integrate MySQL tools with systemd?
It enables automatic service management, restarts on failure, logging via journald, and better operational control of MySQL tools.
How can I check MySQL logs via systemd?
Use journalctl -u mysqld to view logs, or add -f to follow them in real-time.
Can I schedule backups with systemd?
Yes, you can use systemd timers to run mysqldump or other scripts at defined intervals.