13

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

5

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
2

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.

  • 1
    Yes, we discussed this and its limitations on the mailing list: lists.freedesktop.org/archives/systemd-devel/2016-April/… – Jamie Kitson Oct 28 '16 at 16:01
  • 1
    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. – James M. Lay Jan 6 '17 at 13:09
0

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

  • 8
    How is a cron job an improvement over systemd .timer units that OP already knows? – Pavel Šimerda Feb 26 '16 at 7:16
  • 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. – Jamie Kitson Feb 26 '16 at 9:56
  • @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. – Pavel Šimerda Oct 28 '16 at 8:03
  • how do you allow for example to www-data to run systemctl start&stop? – alvaropgl Jun 5 '18 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. – Crossfit_and_Beer Apr 18 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.