DEV Community

Yawar Amin
Yawar Amin

Posted on

How to set up a 20-minute computer break on Linux

IN A previous post, I showed how to set up a scheduled notification every twenty minutes of using the computer, to help follow the 20-20-20 rule to lessen eyestrain.

In this post, I will show the equivalent for Linux. Specifically, using systemd timers. It's very similar in structure to the previous post:

  • Set up a service unit for the notification
  • Set up a timer unit to schedule the service
  • Run commands to enable the timer

First, the service unit:

# ~/.config/systemd/user/twenty.service

[Unit]
Description=20-20-20 rule notifier

[Service]
ExecStart=/usr/bin/notify-send --hint int:transient:1 '20-20-20 rule' 'Take a break!'
Enter fullscreen mode Exit fullscreen mode

This is a declarative equivalent of a shell script that contains the command to run. The important part is the last line, which contains the actual command. Notice that it has a 'hint', a typed key-value pair setting which will ensure that the notification doesn't stick around in the desktop environment's notification history after it disappears from the screen. This hint is documented here.

Remember to place the file in exactly the directory shown above. You will probably need to create it first, with:

mkdir -p ~/.config/systemd/user
Enter fullscreen mode Exit fullscreen mode

Second, the timer unit:

# ~/.config/systemd/user/twenty.timer

[Unit]
Description=20-20-20 rule

[Timer]
OnCalendar=*:0,20,40:00

[Install]
WantedBy=timers.target
Enter fullscreen mode Exit fullscreen mode

This declares a schedule to run the twenty.service unit on (the service name is implicitly understood from the name of the timer file).

The two significant parts of this timer unit are:

  • The OnCalendar= property which sets the schedule. This property can understand various date and time formats, but in this case we are using hh:mm:ss. The hh component is * meaning run every hour; the mm component is 0,20,40 meaning run on the zeroth, twentieth, and fortieth minutes; and the ss component is 00 meaning run on the zeroth second. This translates to: run every twenty minutes.
  • The WantedBy= property which describes when the timer should be enabled on system boot. In this case it says: enable this timer when enabling all other timers on boot.

Finally, enable the timer immediately without needing to reboot the system:

$ systemctl daemon-reload --user # Tell systemd about the new units
$ systemctl enable --user --now twenty.timer # Install the timer
Enter fullscreen mode Exit fullscreen mode

This should set up a symbolic link to the timer, thus installing and enabling it. Note that we are using the --user flag which means enable this timer for this user only, not for the entire system (i.e. all users, which would require admin privileges). The --now flag also immediately activates the timer.

Check that the timer is active:

$ systemctl list-timers --user
NEXT                        LEFT       LAST                        PASSED      UNIT                         ACTIVATES                     
Sun 2020-10-11 20:40:00 EDT 15min left Sun 2020-10-11 20:20:50 EDT 4min 7s ago twenty.timer                 twenty.service                
Mon 2020-10-12 10:33:34 EDT 14h left   Sat 2020-10-10 20:20:28 EDT 24h ago     systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
n/a                         n/a        Sat 2020-10-10 20:17:29 EDT 24h ago     grub-boot-success.timer      grub-boot-success.service     

3 timers listed.
Pass --all to see loaded but inactive timers, too.
Enter fullscreen mode Exit fullscreen mode

This listing shows when the timer will run next.

If you need to disable the timer:

$ systemctl disable --user twenty.timer # Install the timer
Enter fullscreen mode Exit fullscreen mode

If you need to edit and reload the timer, you can disable, then edit, then daemon-reload, then enable as shown above.

Top comments (2)

Collapse
 
jasoons profile image
Jason Smythe

You can try workrave too, it's also for RSI in general and much heavier than yours solution of course 😁

Collapse
 
yawaramin profile image
Yawar Amin

Cool, thank you. I'll check it out sometime.