Programmatically sync dropbox without daemon running
Solution 1:
Using @Rat2000's suggestion, I did this. In one terminal,
while true; do dropbox status; sleep 1; done;
In another terminal:
touch ~/Dropbox/test
The first terminal shows approximately the following output:
Idle
Idle
Idle
...
...
Updating (1 file)
Indexing 1 file...
Updating (1 file)
Indexing 1 file...
...
...
Downloading file list...
Downloading file list...
...
...
Idle
Idle
So we can define a script, using expect
, to do a one-time dropbox sync at controlled times, assuming that the dameon does not report "Idle" until it has finished syncing.
Edit:
This solution seems to work for me:
#!/usr/bin/python
import subprocess, time, re
print "Starting dropbox daemon"
print subprocess.check_output(['dropbox', 'start'])
started_sync = False
conseq_idle = 20
while True:
status = subprocess.check_output(['dropbox', 'status'])
print status
if re.search("Updating|Indexing|Downloading", status):
started_sync = True
conseq_idle = 20
elif re.search("Idle", status):
conseq_idle-=1
if not conseq_idle:
if started_sync:
print "Daemon reports idle consecutively after having synced. Stopping"
time.sleep(5)
else:
print "Daemon seems to have nothing to do. Exiting"
subprocess.call(['dropbox', 'stop'])
break
time.sleep(1)
Notes:
-
Depending on your Dropbox release you might have to replace
elif re.search("Idle", status):
with
elif re.search("Up to date", status):
-
To reduce the impact on system performance you can experiment with utilities such as nice, ionice, and nocache, e.g.:
print subprocess.check_output(['nice', '-n10', 'ionice', '-c3', 'nocache', 'dropbox', 'start'])
Setting the script up with anacron
Of course, I schedule this script to run via anacron, like this:
1 10 dropbox_do_sync su myuser -p -c "python /home/myuser/scripts/dropbox_do_sync.py" >> /home/myuser/logs/anacron/dropbox_do_sync
Solution 2:
Here is a very simplstic way to accomplish this:
I have a slow internet connection, and don't want Dropbox running at all during the day when I am working, but want it running all night when I am supposed to be asleep.
I set up a cron job like this:
At a terminal type crontab -e
Add these lines:
#This line will stop Dropbox at 7 AM every morning:
* 7 * * * dropbox stop
#This line will start dropbox at 10 PM every evening:
* 22 * * * dropbox start
Solution 3:
Using user84207 and Loony answers:
- Create 2 scripts:
startDropbox.sh:
dropbox start
stopDropbox.sh:
result=$(dropbox status)
if [ "$result" = "Up to date" ];
then
dropbox stop
fi
- Add in crontab, with crontab -e
# starts only after each 5 minutes
*/5 * * * * startDropbox.sh
# try to stop every minute
*/1 * * * * stopDropbox.sh