You are not logged in.

#1 2020-04-05 13:05:18

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Systemd service/timer combo to run at reboot AND every 6 hours

I need a service/timer combo that will run every 6 hours and again at a system reboot or shutdown.  I didn't see this exact setup within the systemd.timer and systemd.service man pages, nor from googling.  I have an existing service and timer.

Can I accomplish this with a single timer and a single service?  If so, how?  Changing the service to include an ExecStop= directive doesn't seem right.  I tried adding the reboot.target to the WantedBy= directive but that did not work.  Thanks for pointers.

Last edited by graysky (2020-04-06 11:54:28)

Offline

#2 2020-04-05 15:33:51

koffeinfriedhof
Member
Registered: 2017-11-30
Posts: 91

Re: Systemd service/timer combo to run at reboot AND every 6 hours

Hi!
You could use OnBootSec for the first start after boot and your OnUnitActiveSec for the 6h-rule like this

…
[Timer]
OnBootSec=2min  # delay after boot
OnUnitActiveSec=6h  # every 6h after first activation
Unit=/what/ever/should/run

…

Offline

#3 2020-04-05 17:45:53

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Re: Systemd service/timer combo to run at reboot AND every 6 hours

Yes, but that doesn't cover the on reboot.  I'm thinking it's the service that needs to handle this rather than the timer.

Offline

#4 2020-04-05 18:52:31

koffeinfriedhof
Member
Registered: 2017-11-30
Posts: 91

Re: Systemd service/timer combo to run at reboot AND every 6 hours

It should start 2 minutes after reboot. A full working example I'm currently using looks like this:

[Unit]
Description=Share battery state with home-server

[Timer]
OnBootSec=120
OnUnitActiveSec=120
Unit=BatteryWatcher.service

[Install]
WantedBy=timers.target

The service-unit itself has only a description and an ExecStart-line.

Offline

#5 2020-04-05 19:15:54

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,599

Re: Systemd service/timer combo to run at reboot AND every 6 hours

If you don't have an ExecStart line;

RemainAfterExit=true
ExecStop=/usr/bin/modprobed-db storesilent

if you have (I saw you have..)

ExecStop=/usr/bin/modprobed-db storesilent

or add to your reboot script/alias

/usr/bin/modprobed-db storesilent && systemctl reboot

Offline

#6 2020-04-06 12:16:51

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,734
Website

Re: Systemd service/timer combo to run at reboot AND every 6 hours

Thanks for the replies, all.  Incorporating these suggestions seems to work as expected, although the journalctl seem to be getting spammed with extra messages now:

% journalctl --user -u modprobed-db
Apr 06 07:37:57 ankh systemd[826]: Finished Modprobed-db.
Apr 06 07:39:48 ankh systemd[826]: Stopping Modprobed-db...
Apr 06 07:39:48 ankh modprobed-db[896]: No new modules detected
Apr 06 07:39:48 ankh systemd[826]: modprobed-db.service: Succeeded.
Apr 06 07:39:48 ankh systemd[826]: Stopped Modprobed-db.
Apr 06 08:03:21 ankh systemd[994]: Finished Modprobed-db.

<< set reboot here >>

Apr 06 08:13:26 ankh systemd[994]: Stopping Modprobed-db...
Apr 06 08:13:26 ankh modprobed-db[1123]: No new modules detected
Apr 06 08:13:26 ankh systemd[994]: modprobed-db.service: Succeeded.
Apr 06 08:13:26 ankh systemd[994]: Stopped Modprobed-db.
-- Reboot --
Apr 06 08:15:11 ankh systemd[842]: Finished Modprobed-db.

Last edited by graysky (2020-04-06 12:35:36)

Offline

#7 2020-04-06 16:48:28

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,599

Re: Systemd service/timer combo to run at reboot AND every 6 hours

Yes, it's default these messages are 'spammed' to your journal..
There is a way to push these messages to a different log file if you wish;

Something like:

StandardInput=journal
StandardOutput=file:/path/to/log
StandardError=syslog (edit)
SyslogIdentifier=/usr/bin/modprobed-db storesilent

This may not enough to make it working but in the right direction anyway,
(see) https://www.freedesktop.org/software/sy … put/Output
&
https://www.freedesktop.org/software/sy … put/Output

edit: forget the above code mostly, unless you want to use syslog, this should be enough to put in your timer;

StandardError=file:/path/to/modprobed.log
StandardOutput=file:/path/to/modprobed.log

I never used this approach and I'm not going to, but it is working...
Usually I grep what I like to see and never scroll trough the entire journal..

Last edited by qinohe (2020-04-06 19:33:57)

Offline

Board footer

Powered by FluxBB