CoreOS scheduled tasks
CoreOS uses the Systemd that has built in a bunch of helpful features and tools. For example, instead of using Cron that is the most common way to run scheduled tasks in most of the Linux distributions, Systemd has the Timer unit configuration that schedules the execution of a Service unit.
This past week I had to configure a scheduled backup routine to run everyday at 2 AM. So this examples is what I had to configure on Systemd to run the backup routine daily. The backup script backup-mongodb.sh I have not included in this post because it is just a simple Mongodb backup script.
Basically the simplest way to schedule a service is creating a Timer unit with the same name of the service that is going to be executed by the timer.
Files๐
backup-mongodb.service๐
It requires the mongodb.service is running and it runs the backup script.
[Unit]
Backup of mongodb
.service
mongodb.service
mongodb
[Service]
/bin/bash /home/core/backup-routines/backup-mongodb.sh
[Install]
.target
local
backup-mongodb.timer๐
It also requires the mongodb.service is running and it triggers the backup-mongodb.service every day at 2 AM. Using Persistent=true persists information on last time the timer was triggered. For more information about Timer take a look in the documentation.
[Unit]
2 AM
Runs mongodb backup every day at .service
mongodb.service
mongodb
[Timer]
02:00
true
[Install]
.target
local
Configuring backup schedule๐
After created those files we just need to load them into the CoreOS machine and start the timer.
BACKUP_NAME="backup-mongodb"
# removes from coreos these configurations in case they already exist there
# loads the service and timer units
# starts the timer
Status๐
Looking the list-units you are going to see the timer in active-waiting and the service inactive-dead status. Don't worry about the service status. The timer is to start it in the configured time.
|