1

I want to automate a process after Dropbox does update changes made to a directory (changes could be made in a subdirectory too).

In order for this to work the monitoring script should wait for Dropbox to report the synchronization status as done, as I do not want to start the action when only some files where updated, or to start multiple executions in parallel.

This has to work on Linux, where I do have the Dropbox headless daemon already configured to run. I also have the dropbox.py script which seems to be able to report if the syncronization was made.

./dropbox.py status
Up to date

I was considering using a cron job but I do want to minimize the delay between the moment when dropbox finishes the sync and the moment I trigger the execution. Using Cron every minute does not seem a good approach, I would prefer something that is using some kind of triggers, not a dumb polling.

3 Answers 3

2

I don't know what triggers are available with Dropbox. That seems like the sort of thing you'd contact their support department about, since they are after all a commercial service.

If you find yourself stuck with polling, you could wrap your python script in a loop that checks for a state change.

#!/usr/bin/env bash

postprocess() {
  # This function contains stuff you run after sync is complete.
}

# Initialize a flag.
dirty=true

# How many seconds between checks?
interval=1

# Poll every second. You could increase this number to reduce impact.
while sleep $interval; do

  if $dirty; then
    if /path/to/dropbox.py status | grep -q 'Up to date'; then
      dirty=false
      postprocess
    else
      # Still syncing, no state change, try again in $interval seconds
      continue
    fi
  else
    if /path/to/dropbox.py status | grep -q 'Up to date'; then
      # No updates happening, no state change
      continue
    else
      dirty=true
    fi
  fi

done

I haven't tested this obviously, but perhaps you can adapt it to your needs. Note that a shorter interval means that the script will more accurately detect changes, a longer one means it may miss them. Even at a 1 second interval, I would expect the dropbox client could start and complete a change within 1 second, so you might miss things.

If you know of a way to detect a trigger, please post your code here, and you'll find a crowd of people happy to help make it work. A question without code to debug is .. well, practically off-topic! :)

Sign up to request clarification or add additional context in comments.

Comments

1

Use the inotify system. There's a command line tool inotifywait, and a python library. You can be notified of a file being created, read, closed after write, etc.

2 Comments

How would inotify know that the synchronization is complete? I thought it would only provide notification of a change.
I imagined that the proper solution would involve inotify. When it comes to knowing when the sync was finishes, I can check the dropbox.py response.
0

Check out fswatch. I've used it before to do something similar. This is a line from a program I used it on.

fswatch --print0 -e '/\.' $dir | xargs -0 -n1 ./client.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.