# API: Check when sync is "finished"? LocalIndexUpdated false positive?

**URL:** https://forum.syncthing.net/t/api-check-when-sync-is-finished-localindexupdated-false-positive/22098
**Category:** Support
**Created:** [May 8, 2024, 1:39am UTC](https://forum.syncthing.net/t/api-check-when-sync-is-finished-localindexupdated-false-positive/22098 "2024-05-08T01:39:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![enoryw](https://forum.syncthing.net/user_avatar/forum.syncthing.net/enoryw/32/16595_2.png) [@enoryw](https://forum.syncthing.net/u/enoryw)
#### Post date: [May 8, 2024, 1:39am UTC](https://forum.syncthing.net/t/api-check-when-sync-is-finished-localindexupdated-false-positive/22098/1 "2024-05-08T01:39:31Z")

</div>

I have a shell script using the events API and am struggling to check whether sync is “finished” more prior to system shutdown and also on system startup. Technically a sync is never finished because it’s syncing continuously, but for the purposes above, the best definition _probably_ means that “within say a minute of timeout, at least a file was synced (indicating at least any remaining files should be synced at that moment as well) and then no more files are synced”.

To be clear, `LocalIndexUpdated`/`RemoteIndexUpdated` is emitted when a file is synced (i.e. changed)? Brief test shows that this seems to be working as expected except this: a `LocalIndexUpdated` emitted for 2 particular directories (one a parent of the other) that were never touched (including any of its files) and I’m not sure why. To confirm, the GUI lists “recent changes” and it labeled these directories as “modified dirs” while the rest of the recently changed files listed do not involve this path. Its modified time according to `ls` is older, i.e. it does not reflect what is reported by Syncthing.

Any ideas? Is simply checking for `LocalIndexUpdated`/`RemoteIndexUpdated` within a timeout period the best way to check for syncing completion? I was also considering `StateChanged` but its `.data.from` and `.data.to=idle` but `.data.from` seems to have more states besides what’s mentioned in the docs, e.g. `sync-waiting`, `sync-waiting`, and `sync-preparing` so doesn’t be so straightforward.

---

<div class="post-metadata">

### Author: ![calmh](https://forum.syncthing.net/user_avatar/forum.syncthing.net/calmh/32/15311_2.png) [@calmh](https://forum.syncthing.net/u/calmh)
#### Post date: [May 8, 2024, 5:46am UTC](https://forum.syncthing.net/t/api-check-when-sync-is-finished-localindexupdated-false-positive/22098/2 "2024-05-08T05:46:39Z")

</div>

This is indeed surprisingly tricky. I would suggest looking at state changes and folder summary events. Folders being idle (no “syncing” state within recent memory, perhaps) and folder summary saying 100% completion is probably as good as it gets right now.

It’s trickier if you also want to worry about peers having synced all changes that might have been made locally.

---

<div class="post-metadata">

### Author: ![Martchus](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@Martchus](https://forum.syncthing.net/u/Martchus)
#### Post date: [May 8, 2024, 12:16pm UTC](https://forum.syncthing.net/t/api-check-when-sync-is-finished-localindexupdated-false-positive/22098/3 "2024-05-08T12:16:51Z")

</div>

I also noticed that this is tricky when developing Syncthing Tray. There I also ended up reading folder summary events like @calmh has already mentioned. For remote devices one could query `db/completion` on `RemoteIndexUpdated`-events (if my memory serves me well).

Note that `syncthingctl` might already do what you want, see [Shutdown computer when sync is done - #3 by Martchus](https://forum.syncthing.net/t/shutdown-computer-when-sync-is-done/14190/3). It only covers the local completion, though.

---

<div class="post-metadata">

### Author: ![system](https://forum-uploads.syncthingcdn.net/original/2X/0/0b50e0a202b22ae6a67190759e8c868805b8ed9f.png) [@system](https://forum.syncthing.net/u/system)
#### Post date: [June 7, 2024, 12:17pm UTC](https://forum.syncthing.net/t/api-check-when-sync-is-finished-localindexupdated-false-positive/22098/4 "2024-06-07T12:17:24Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
