# Prevent Systemd Timers from Overlapping

Sometimes, if we have a repeative task running with Systemd timer, imagine a scenario where before the process was even finished, the timer starts running yet another process, and this is where the Systemd timer can have overlapping processes, and to avoid this, we can use `flock` command, example below -

## Example: A Simple Systemd Service File

```
[Unit]
Description=Rclone Sync for an S3 Bucket.
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
User=ubuntu
Group=ubuntu
ExecStart=/usr/bin/flock /var/lock/rclone-sync.lock -w 30 /usr/bin/rclone sync --progress s3-bucket:content /home/ubuntu/rclone-sync
```

Here, the `flock` command runs rclone sync with a lock-file at `/var/lock/rclone-sync.lock`, now if the earlier execution wasn't finished, the lock file will be there, and the `flock` command would wait for 30 seconds (courtesy to `-w 30` argument) before it exits with an error.

***Source(s):***

* [CleverUptime](https://cleveruptime.com/docs/commands/flock)
* [Mattias Geniar Blog](https://ma.ttias.be/prevent-cronjobs-from-overlapping-in-linux/)
* [Bashscript.net](https://bashscript.net/using-flock-in-bash-scripts-manage-file-locks-and-prevent-task-overlaps/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://til.devjugal.com/linux/systemd/prevent-systemd-timers-from-overlapping.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
