GSoC : Creating timers in Cockpit

Harish Anand
4 min readAug 15, 2016

--

In this blog, I will provide a description of how to create a (systemd) timer using Cockpit. Also I will mention about the proposed feature sets and what actually got merged in Cockpit.

How to create timers

  1. Repeat hourly timers

Hourly timers are used for repeating events on the mentioned minutes every hour.

Here we set up a timer that repeats at 5 min and 26 min past every hour.

This command creates a tmp/date file that contains the time when this timer runs.

The contents of hourly_timer.service file and hourly_timer.timer file are given below:

[Unit]
Description=Hourly timer
[Service]
ExecStart=/bin/sh -c '/bin/date >> /tmp/date'
[Install]
WantedBy=default.target
[Unit]
Description=Hourly timer
[Timer]
OnCalendar=*-*-* *:5,26:00

2. Repeat daily timers

Daily timers are used to repeat events on the mentioned hours and minutes every day. Here we create a daily timer that repeats every day at 1:20, 2:40, 21:16. The daily_timer.service and daily_timer.timer file’s contents are as follows

[Unit]
Description=Daily Timer
[Service]
ExecStart=/bin/sh -c '/bin/date >> /tmp/date'
[Install]
WantedBy=default.target
[Unit]
Description=Daily Timer
[Timer]
OnCalendar=*-*-* 1:20:00
OnCalendar=*-*-* 2:40:00
OnCalendar=*-*-* 21:16:00

3. Repeat weekly timers

Weekly timers are used to repeat events on the mentioned week-days every week. Here we create a weekly timer that repeats every week on Monday at 10:1, Thursday at 12:00, Friday at 01:10.

Contents of weekly.service[Unit]
Description=Weekly Timer
[Service]
ExecStart=/bin/sh -c '/bin/date >> /tmp/date'
[Install]
WantedBy=default.target
Contents of weekly.timer[Unit]
Description=Weekly Timer
[Timer]
OnCalendar=Mon *-*-* 10:1:00
OnCalendar=Thu *-*-* 12:0:00
OnCalendar=Fri *-*-* 1:10:00

Repeat monthly timer

Monthly timers are used for repeat events on mentioned dates every month.

This timer runs on 21st and 24th of every month at 2 AM and 4 AM respectively. The contents of monthly.service and monthly.timer are

[Unit]
Description=Monthly
[Service]
ExecStart=/bin/sh -c '/bin/date >> /tmp/date'
[Install]
WantedBy=default.target
[Unit]
Description=Monthly
[Timer]
OnCalendar=*-*-21 2:0:00
OnCalendar=*-*-24 4:0:00

Repeat yearly timer

Yearly timer repeats events on the specific days in every year. The contents of the yearly_timer.service and yearly_timer.timer are :

[Unit]
Description=Yearly Timer
[Service]
ExecStart=/bin/sh -c 'while true; do sleep 5; done'
[Install]
WantedBy=default.target
[Unit]
Description=Yearly Timer
[Timer]
OnCalendar=*-08-15 15:0:00
OnCalendar=*-09-06 1:22:00

Boot timer
A boot timer is setup which starts 2 seconds after boot. The contents of boot.service and boot.timer are as follows:

[Unit]
Description=Boot Timer
[Service]
ExecStart=/usr/bin/sh -c 'echo hello >> /tmp/hello'
[Install]
WantedBy=default.target
[Unit]
Description=Boot Timer
[Timer]
OnBootSec=2sec

Display of timers

The timer page in Cockpit displays the next run and last run time of the timers.

Proposed feature’s as part of GSoC were:

➔ A working systemd timer interface to schedule unit services in Cockpit web interface.

➔ Addition and modification of the timer properties to create, modify and schedule tasks.

➔ Implement a timer interface as described in https://trello.com/c/1B2lZViZ/74-timers-and-cron.

Proposed features that got merged as part of Cockpit includes:

  1. Creation of calendar and monotonic timers without using D-BUS.
  2. Display of next run and last run time of existing timers like in the mock-up.

Extra features that could be added include :

  1. Modification and deletion of timers.
  2. Creation of timers for already existing services.

https://github.com/cockpit-project/cockpit/pull/4645

--

--