User Tools

Site Tools


ubuntu:systemd:timers

Ubuntu - Systemd - Timers

To have a systemd service not start automatically at system boot time, but instead to run at a specific time.

  • Create a systemd timer for the systemd service.

Example

Create a systemd service (/etc/systemd/system/iris.service) for the Iris eye protection software.

/etc/systemd/system/iris.service
[Unit]
Description=Iris Eye Protection Software 
After=lightdm.service
 
[Service]
  Type=oneshot
  Environment=DISPLAY=:0
  User=your_username
  ExecStart=/bin/bash -c 'DISPLAY=:0 xhost 127.0.0.1 && /opt/iris/Iris.sh'
  KillSignal=SIGINT
  Restart=on-failure
  RestartSec=2
 
[Install]
  WantedBy=multi-user.target

To have it automatically start at boot time

sudo systemctl enable iris.service

To start only at night

Create a systemd timer.

sudo vi /etc/systemd/system/iris.timer

…add the following lines in this file:

/etc/systemd/system/iris.timer
[Unit]
Description=Iris Night Mode Timer
Requires=iris.service
 
[Timer]
Unit=iris.service
OnCalendar=*-*-* 18:00:00
 
[Install]
WantedBy=timers.target

NOTE: This systemd timer will start the iris.service at 18:00:00 every day.


Enable this systemd timer

sudo systemctl enable iris.timer

Check the status of this timer

sudo systemctl status iris.timer

returns:

● iris.timer - Iris Night Mode Timer
     Loaded: loaded (/etc/systemd/system/iris.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Sat 2023-06-03 17:32:12 +00; 24min ago
    Trigger: Sat 2023-06-03 18:00:00 +00; 22h left
   Triggers: ● iris.service
ubuntu/systemd/timers.txt · Last modified: 2023/06/03 10:44 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki