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 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.



It requires the mongodb.service is running and it runs the backup script.

Description=Backup of mongodb

ExecStart=/bin/bash /home/core/backup-routines/



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.

Description=Runs mongodb backup every day at 2 AM



Configuring backup schedule

After created those files we just need to load them into the CoreOS machine and start the timer.


# removes from coreos these configurations in case they already exist there
fleetctl destroy $(fleetctl list-units | grep "^$BACKUP_NAME")

# loads the service and timer units
fleetctl load $BACKUP_NAME.service
fleetctl load $BACKUP_NAME.timer

# starts the timer
fleetctl start $BACKUP_NAME.timer


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.

fleetctl list-units | grep backup-mongodb

UNIT                        ACTIVE          SUB
backup-mongodb.service     inactive        dead
backup-mongodb.timer       active          waiting