30

I want to start and stop a systemd.service at specific times. Presumably I will use a .timer unit to start the job, but is there a built in way to stop the job after a specific duration, or at a specific time, or do I have to create a second .timer unit that execs the stop?

Thanks

3 Answers 3

17

To stop a service A with a timer, you can create a service B of type oneshot that it will be in conflict with, then use a timer to start the service B.

If a unit has a Conflicts= setting on another unit, starting the former will stop the latter and vice versa. (source)

A.service:

[Unit]
Conflicts=B.service
...

B.service:

[Unit]
Description=B service description

[Service]
Type=oneshot
ExecStart=/bin/echo ''

B.timer:

[Timer]
AccuracySec=1
OnActiveSec=10

[Install]
WantedBy=timers.target

The following will stop service A after 10 seconds.

systemctl start A.service
systemctl start B.timer
3
  • What is the advantage of this compared to just calling systemctl restart A in B.service?
    – balki
    Apr 16, 2020 at 15:13
  • 1
    to exclusively use systemd's files without calling explicitly systemctl (user permission and so on)
    – Alexis
    Nov 24, 2021 at 15:02
  • You could just as well put the Conflicts entry in B.service, right? That might make it clearer what B is for to anyone reading its unit file (although you should probably call it something like A-stop anyway). Could you actually put the Conflicts in the timer file? A timer is a unit, right? I wonder if you can get away without defining B.service at all, eg by adding Unit=dbus to B.timer! Aug 23 at 17:46
15

You could use a couple of cron jobs:

 # ┌───────────── min (0 - 59) 
 # │ ┌────────────── hour (0 - 23)
 # │ │ ┌─────────────── day of month (1 - 31)
 # │ │ │ ┌──────────────── month (1 - 12)
 # │ │ │ │ ┌───────────────── day of week (0 - 6)
 # │ │ │ │ │
 # │ │ │ │ │
   * * * * *  systemctl start $SERVICE.service
   * * * * *  systemctl stop $SERVICE.service

More info on cron: https://en.wikipedia.org/wiki/Cron, https://wiki.archlinux.org/index.php/Cron

5
  • 14
    How is a cron job an improvement over systemd .timer units that OP already knows? Feb 26, 2016 at 7:16
  • 1
    I could, yes, but my question really is how to do this properly with systemd? I assume there must be some standard way of getting a job to stop at a specific time or after a certain duration. Feb 26, 2016 at 9:56
  • 4
    @JamieKitson To be honest I don't think there actually needs to be such a feature beyond cron and systemd timers. Most systemd installation will never use such features and there's nothing wrong with running systemctl using cron, systemd timers and whatever you like. In my opinion this answer is as valid as any other answer. Oct 28, 2016 at 8:03
  • how do you allow for example to www-data to run systemctl start&stop?
    – alvaropgl
    Jun 5, 2018 at 12:56
  • @alvaropgl Your comment has nothing to do with users (www-data) and what limited access they may have to run/not-run processes (systemctl), which is the topic. Please start a new topic. Hint: you probably want to look at making an API to do things you want, rather than your current approach of enabling more responsibility + scope on the www-data user. Apr 18, 2019 at 11:20
8

There indeed is another way to stop a service after certain runtime configured in the .service file.

RuntimeMaxSec=...

You may not like the fact that the service is considered failed but that's a more or less logical result of killing a long-running service.

To get a better answer you may want to explain your rationale to use such an unusual feature. Services are generally meant to run forever or until they are explicitly stopped, not just for a fixed amount of time.

4
  • 3
    Yes, we discussed this and its limitations on the mailing list: lists.freedesktop.org/archives/systemd-devel/2016-April/… Oct 28, 2016 at 16:01
  • 8
    Not unusual. What about running a resource-demanding service, perhaps SETI, at night time only, when the server gets the least traffic. Also, where I work, we have an alert daemon that's designed to wake the support team via their phones when there's an exception on the servers. We really don't want that annoying thing running when we're actually awake because those servers have problems left and right during peak use. Jan 6, 2017 at 13:09
  • 1
    Or just a service that creates a file for the day and we want it to restart it at a certain time to create a new file and clean/reset all the states of the app. I see lots of reasons for restarting a service at a specified time.
    – Alexis
    Nov 24, 2021 at 15:04
  • If you want to stop the service at a particular time, then this isn't a great solution, because if you restart the service during the day, the countdown will start over. Aug 23 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .