Systemd – Does Timer Unit Skip Next Run if Process Hasn’t Finished?

cronsystemd

I want to use systemd to run a command every 5 minutes. However, there is a risk that occasionally the task may take longer than 5 minutes to run. At that point, will systemd start a second instance of the command i.e. will I end up with 2 processes running?

Is it possible to tell systemd not to start a second process if the first hasn't completed? If not, what are some good workarounds?

Note: I hope the answer is "That's the default behavior. It just isn't documented." If this is the situation, can someone tell me how to file a bug against their docs?

Note: Cron has a similar issue which is discussed in https://unix.stackexchange.com/a/173928/11244. I'm looking for the systemd equivalent.

Best Answer

This is the default (and the only) behavior. It is not explicitly documented, but is implied by systemd's operation logic.

systemd.timer(5) reads:

For each timer file, a matching unit file must exist, describing the unit to activate when the timer elapses.

systemd(1), in turn, describes the concept of unit states and transitions between them:

Units may be "active" (meaning started, bound, plugged in, ..., depending on the unit type, see below), or "inactive" (meaning stopped, unbound, unplugged, ...), as well as in the process of being activated or deactivated, i.e. between the two states (these states are called "activating", "deactivating").

This means that the triggering of a timer leads to "activation" of the matching unit, i. e. its transition to the "active" state.

If the matching unit is already "active" at the time of "activation" (for a service unit, this means "the main process is still running", unless the service unit has Type=oneshot and RemainAfterExit=true), it should be obvious that no action will be taken.

Related Question