# December 13, 2024

## Things That I Discovered

### 1. Prevent cronjobs from overlapping

* I recently started running Systemd timer for a recurring task; rclone sync, and wanted bare minimum time to re-run it, but here comes a caveat, if the execution doesn't finish in time, a new task might be started, resulting in overlap, and if the task is handling data, say database for an example. That can lead to data loss/corruption. We can use `flock` command to overcome this.
* **Example:**

  ```
  flock /var/lock/rclone-sync.lock -w 30 rclone sync <SOURCE> <DESTINATION>
  ```
* Here, `flock` command would check for a lock file at `/var/lock/rclone-sync.lock` and wait for 30 seconds before it exits with an error if a lock file is found.
* Read more at my TIL [Website](https://til.devjugal.com/linux/systemd/prevent-systemd-timers-from-overlapping).
* **Reference(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/)
