Trying to check for sync completion with the API

I want to check that my system is freshly synced on system startup to know that I’m not working with old files that haven’t been updated from the sync.

At first I tried polling for completion:

curl -sk -H "X-API-Key: $apikey" -X GET "$folder_id" | jq .completion

but even at .1 seconds, I tested by creating a 1G file on the remote machine and it never reported anything under 100%. On my local system (Linux), all I can see is no file, and then the 1G file in an instant. Even with inotify, I noticed there’s a delay from when I created the file to when it starts syncing.

Quick google search I see there’s also an event for completion but I’m not sure how this is useful for my purposes–it tracks syncs as they are happening but I guess not when “a successful sync was recently completed”.

At the moment, the only thing that seems straightforward is a last scan time which can at least indicate when a sync should have started but not necessarily when it was completed and without errors.

My attempt with a shell script. Any help is much appreciated.

Your completion call sounds correct, though “are we in sync” is a tricky question to answer. The answer might be out of date by the time you get it, and we can only answer it based on what we know – if a device is disconnected, it might not know it’s missing files. If you want a snapshot answer for the time you ask the question I think it’s about as good as it gets.

To catch things as they happen you can listen for the StateChanged event (folders going from syncing to idle) and the FolderSummary event (to get completion percentage as it changes) – though this is also periodic rather than strictly realtime.

I went about getting that answer using this tool:

You put the file in your syncthing’s config folder.

It will yield output like below which you can then parse to see if everything is complete. So the “Needs” column will suit your use case I imagine. If everything equals ‘0 B’ your dealing with the latest files.

Device           Status   Sync     Download  Upload   Needs
xxxxxxxx         OK       100.0%   3.5 kB    4.2 kB   0 B
xxxxxxxx         OK       100.0%   65 kB     62 kB    0 B
xxxxxxxx         OK       100.0%   3.8 kB    3.9 kB   0 B
*xxxxxxxx        Myself   100.0%   0 B       0 B      0 B
xxxxxxxx         OK       100.0%   5.9 MB    48 GB    0 B
xxxxxxxx         OK       100.0%   2.6 MB    10 GB    0 B

As a bash script, here’s a working if fairly clunky example:


INSYNC_DATA=$(/home/youruser/.config/syncthing/stc-arm64-linux | sed -n '/Device/,//p' | awk '//{ printf "%-10s %-5s %s\n", $1, $8, $9, $10 }' | grep "0     B" | wc -l)

INSYNC_PERCENT=$(/home/youruser/.config/syncthing/stc-arm64-linux | sed -n '/Device/,//p' | awk '//{ printf "%-10s %-5s %s\n", $1, $3, $4 }' | grep "100.0%" | wc -l)

if [[ $INSYNC_DATA =~ "your total number of devices" && $INSYNC_PERCENT =~ "your total number of devices" ]] 
    ## action for all in sync
    ## action for out of sync

Hope that helps you :+1:

1 Like

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