7

I want to delay the start of a service if a file exists (instead of fail the service if the file exist, as with ConditionPathExists=) but did not find anything in unit documentation

Is it technically possible with systemd ? How ?

3 Answers 3

9

As far as I can tell ConditionPathExists= does not put the unit into a "failed" state. ConditionPathExists= only skips the unit.

Unit files may also include a number of Condition…= and Assert…= settings. Before the unit is started, systemd will verify that the specified conditions are true. If not, the starting of the unit will be (mostly silently) skipped. Failing conditions will not result in the unit being moved into the "failed" state.

One way I found to delay the start of a unit, based on the existence of a file, is to test for the file with ExecStartPre=, then Restart=on-failure and RestartSec=. This does put the unit into a failure state, but it will keep retrying until success. For example:

[Service]
# Should cause failure if file exists
ExecStartPre=/usr/bin/test ! -f afile
ExecStart=mycommand

# Restart on failure. Keep trying to create backup.
RestartSec=10m
Restart=on-failure
1
  • Works without the negation to wait for a config file to be created! Thanks! Nov 3, 2021 at 17:59
8

using just one unit

Put TimeoutStartSec=infinity in the unit file and configure ExecStart= with a script like

#! /bin/bash

TIMEOUT=1000

test -f /path/to/testfile && sleep "$TIMEOUT"

exec /path/to/service/binary plus arguments

This cannot be done (in a useful way) with ExecStartPre=, see man systemd.service:

Note that ExecStartPre= may not be used to start long-running processes. All processes forked off by processes invoked via ExecStartPre= will be killed before the next service process is run.

using a helper unit

If you want to do this with systemd "alone" then you can create a helper unit check_and_wait.target. This one gets the entries

# check_and_wait.target
[Unit]
TimeoutStartSec=infinity
ConditionPathExists=/path/to/testfile
ExecStart=/usr/bin/sleep 1000
RemainAfterExit=yes

The main unit gets these entries:

Wants=check_and_wait.target
After=check_and_wait.target
4
  • 1
    Surely it can use ExecStartPre, just move binary into ExecStart. What goes wrong?
    – sourcejedi
    Aug 26, 2017 at 13:47
  • Does this mean it is not possible with systemd and necessarily require a shell script (bash or other) ?
    – Zulgrib
    Aug 26, 2017 at 14:45
  • @Zulgrib As usual there are several possibilities. I have added another to my answer. Aug 26, 2017 at 15:22
  • You can use ExecStartPre (since it's not sleeping forever) ... methinks...
    – rogerdpack
    Jul 9, 2019 at 17:26
0
[Service]
ExecStartPre=bash -c "while [ -f /path/to/file ]; do sleep 1; done"
ExecStart=your_command
1
  • 1
    Welcome to the site, and thank you for your contribution. Please note however that this would inhibit starting the service until the file disappears, not delay it for a fixed amount of time if the file is present (which seems to be what the OP wands).
    – AdminBee
    Nov 3, 2020 at 14:03

You must log in to answer this question.

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