sorin
sorin

Reputation: 170390

How to trigger a script execution after Dropbox updates a specific directory?

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.

Upvotes: 1

Views: 1099

Answers (3)

ghoti
ghoti

Reputation: 46826

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! :)

Upvotes: 2

meuh
meuh

Reputation: 12255

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.

Upvotes: 1

ollien
ollien

Reputation: 4766

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

Upvotes: 0

Related Questions