Richard
Richard

Reputation: 10648

how do I configure an end time for systemd timers

I'm using CoreOS and SystemD timers to run my reports...

I have certain monitoring reports that need to run every 3 hours for the next 12 hours, running on the half hour. That's pretty simple to implement if I want to be awake at 1230 to stop the reports.

Is there a "terminate timer" option or something similar in the systemd timers>

Upvotes: 6

Views: 2808

Answers (4)

esguti
esguti

Reputation: 234

Use RuntimeMaxSec can result in the service not stopping at the right time. Per example, if you want to start your service at a specific time (8AM) and one day you have to start the service manually due to a failure. In that case, the service will not finish at the time you expect, it will finish at timestart+RuntimeMaxSec.

To solve this issue, you can create another service with parameter Conflicts=test.service:

[Unit]
Description=Stop Test service
Conflicts=test.service

[Service]
Type=simple
ExecStart=/bin/bash -c 'echo "stopping test service"'
WorkingDirectory=/root
StandardOutput=inherit
StandardError=inherit
Restart=on-failure
RestartSec=10
User=root

and include a timer for executing the stop service at a specific time:

[Unit]
Description=Run stop test service

[Timer]
# Run every day at 23:55h
OnCalendar=Mon..Sun 23:55

[Install]
WantedBy=timers.target

Because the service stop is in conflict with the service test, the first will stop the latter if it is running.

Upvotes: 0

whng
whng

Reputation: 236

It is possible to terminate systemd services after a certain amount of time.

Use the RuntimeMaxSec option for Type=simple services:

[Unit]
Description=Terminate simple Service Test

[Service]
Type=simple
RuntimeMaxSec=5
ExecStart=/bin/sleep 10

Use the TimeoutStartSec for Type=oneshot services:

[Unit]
Description=Terminate oneshot Service Test

[Service]
Type=oneshot
TimeoutStartSec=5
ExecStart=/bin/sleep 10

Both services will be terminated after 5 seconds before the sleep timer reaches 10 seconds.

Details on RuntimeMaxSec and TimeoutStartSec can be found here: https://www.freedesktop.org/software/systemd/man/systemd.service.html

Upvotes: 1

Mark Stosberg
Mark Stosberg

Reputation: 13401

You can simply write another timer that is scheduled to run at the shutdown times you would like. They can run a service which stops the you want to top, by running ExecStart=/bin/systemctl stop other.service in the service file called your shutdown timer.

I reviewed the docs for JobTimeoutSec from man systemd.unit and doesn't seem like quite the right tool for the job.

Upvotes: 0

Six
Six

Reputation: 5512

I believe you can add the following to the service file's [Unit] directives:

JobTimeoutSec=43200

You could probably just write a wrapper script to terminate the process after 12 hours or use timeout 12h COMMAND [ARG]...

Upvotes: 0

Related Questions