Systemd.timer OnUnitActiveSec help

I have a couple scripts that I’d like to run 30 mins after boot and 2 hours thereafter. So in view of that I decided to create a systemd timer unit. I’d like to think all the parameters set are correct, but after the first run it disables itself and does not wake up after time has elapsed.

Given below is a simple test case I’m using for debug.

Steps

  1. Create script to be run.

/home/user1/timer_test.sh

#!/bin/bash

echo “$(date): Run complete”

  1. Grant execute on the script

chmod u+x timer_test.sh

  1. Create timer unit

sudo vim .config/systemd/user/test.timer

[Unit]
Description=test timer
RefuseManualStart=false # Allow manual starts
RefuseManualStop=false # Allow manual stops

[Timer]
Persistent=true
OnBootSec=30m
OnUnitActiveSec=1m
Unit=test.service

[Install]
WantedBy=timers.target

  1. Create test.service

sudo vim .config/systemd/user/test.service

[Unit]
Description=test timer accuracy

[Service]
Type=simple
ExecStart=/home/user1/timer_test.sh

[Install]
WantedBy=default.target

  1. Enable it

systemctl --user enable test.service
#Created symlink /home/user1/.config/systemd/user/default.target.wants/test.service → /home/user1/.config/systemd/user/test.service.
systemctl --user start test.service

However, when I look at its status -

○ test.service - test timer accuracy
Loaded: loaded (/home/user1/.config/systemd/user/test.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2022-03-25 14:51:37 IST; 18min ago
TriggeredBy: ○ test.timer
Process: 70897 ExecStart=/home/user1/timer_test.sh (code=exited, status=0/SUCCESS)
Main PID: 70897 (code=exited, status=0/SUCCESS)
CPU: 6ms

Mar 25 14:51:37 wrapper systemd[4520]: Started test timer accuracy.
Mar 25 14:51:37 wrapper timer_test.sh[70897]: Friday 25 March 2022 02:51:37 PM IST: Run complete

If anyone has ideas on how to debug/fix and can share them, would appreciate it. If more information is needed let me know.

remove these lines.

Better use OnUnitInactiveSec

You might want to remove these lines form your service.

enable and start the timer, not the service. The service is started by the timer, not the other way around.

You can list timers and check the status of the timer.

systemctl --user status test.timer

and

systemctl --user list-timers
2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.