Automate Dropbox ON/OFF on Mac

Hi great community!
We have 8 iMacs (at our school) with Logic and we use Dropbox to sync files over these computers so that students can work on any of the computers regardless of which one the used last week, so to speak.
However Dropbox slows down the Macs significantly. Is there a smart way to automate this so that every day at midnight or so Dropbox turns on and sync files - and then shuts completely off before morning?

Do check the “Enable Lan sync” Dropbox function to speed up local network syncing. Needs to be activated on all Dropbox clients.

Alternatively use a Synology NAS in the network and set up folder sync/share with each workstation. This way all traffic is local and instant without the uploading and downloading and having to use the “fat” Dropbox client.

1 Like

Yes! You can definitely do this, and it’s pretty easy, and free.

First we’ll need to look at how to launch and quit Dropbox, and then we’ll need to set it up to happen automatically.

How to automatically launch Dropbox

Fortunately this one is easy:

/usr/bin/open -a Dropbox

That’s it! The app will open, just as if someone had double-clicked the app. And the good news is that if the app is already running, this command won’t hurt anything.

How to automatically quit Dropbox

Note: You should be able to quit Dropbox (or any other Mac app) with

osascript -e ‘quit application “Dropbox”’

which is the “nice” way of quitting an app, and is suppose to allow the app to close down nicely… but because Dropbox is made by people who think they know better than their users, they prevent the Dropbox app from being quit in this manner.

Attempting to do so will result in an error: execution error: Dropbox got an error: User canceled. (-128) which is supposed to be used if you try to quit an app but the user decided not to (because it prompted them to save a file or something. But the Dropbox developers are unfamiliar with the concepts of “shame” and “wrong” so they abuse the privilege of being installed on our computers.

Therefore, we are left with pkill with is not a nice way to quit an app, but we have no choice because of the ego of Dropbox’s developers.

So, to stop Dropbox, you need to run this command:

/usr/bin/pkill Dropbox

Now, mind you, because of what I explained about, there is no way to say to Dropbox “Ok, finish syncing any files in progress, and then quit. When you issue this command, Dropbox stops immediately. Of course, it should finish syncing automatically next time it runs.


How to Automate This

launchd is the preferred way of scheduling things on your Mac. It comes built-in, and it’s free. (However, I highly recommend LaunchControl: The launchd GUI if you do a lot of launchd work.)

Using launchd to quit Dropbox

Copy the code block below into a text file, and save it with a name such as:

com.tjluoma.quit-dropbox-at-time.plist

(Don’t ask me why they name launchd files this way, they just do. You can call it pretty much anything you want, as long as it ends with .plist.)

Also!!! Make 100% sure there are no blank lines before the <?xml line at the top of the file !!)

<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0”>
<dict>
	<key>Label</key>
	<string>com.tjluoma.quit-dropbox-at-time</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/bin/pkill</string>
		<string>Dropbox</string>
	</array>
	<key>RunAtLoad</key>
	<false/>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>6</integer>
		<key>Minute</key>
		<integer>45</integer>
	</dict>
</dict>
</plist>

Now, that looks pretty complicated, I’m sure (I used LaunchControl to create it for me), but really it’s pretty simple. If you look you will see the same pkill command is in there, it’s just formatted in a way for a computer to understand it.

You will notice that the hour is set to 6 and the minute is set to 45 which means that at 6:45 a.m., Dropbox will be quit. You can change that time by changing those numbers appropriately. (Just remember the hour must be 0-23 as there is no AM/PM in launchd.)

Using launchd to launch Dropbox

Copy the code block below into a text file, and save it with a name such as:

com.tjluoma.launch-dropbox-at-time.plist

<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0”>
<dict>
	<key>Label</key>
	<string>com.tjluoma.quit-dropbox-at-time</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/bin/open</string>
		<string>-a</string>
		<string>Dropbox</string>
	</array>
	<key>RunAtLoad</key>
	<false/>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>23</integer>
		<key>Minute</key>
		<integer>45</integer>
	</dict>
</dict>
</plist>

Same idea here. Notice that open -a Dropbox is still at the heart of the command.

You will notice that the hour is set to 23 and the minute is set to 45 which means that at 11:45 p.m., Dropbox will launchd. You can change that time by changing those numbers appropriately. (Just remember the hour must be 0-23 as there is no AM/PM in launchd.)

Installation of the .plist Files

launchd requires you to put files in specific folders, or else they will have no effect.

Normally you would want to put these in ~/Library/LaunchAgents where ~/ refers to the user’s home folder, which you probably know.

But in a Computer Lab setting, you don’t want to have to install this separately for each user account. That would be a pain to manage. Any time you changed it, you would have to change it for each user on each Mac. That would obviously get tedious very quickly.

Fortunately, Apple does have a place for launchd items which should be run for every user who logs in. Instead of ~/Library/LaunchAgents you want to use /Library/LaunchAgents (note the lack of ~ which is vital!)

  1. Edit the two files com.tjluoma.launch-dropbox-at-time.plist and com.tjluoma.quit-dropbox-at-time.plist to have the times that you want to use (assuming that you don’t want to use the times I’ve set).

  2. You will need to move them into /Library/LaunchAgents which you can only do with sudo like so:

sudo mv -vn 'com.tjluoma.launch-dropbox-at-time.plist' '/Library/LaunchAgents'

sudo mv -vn 'com.tjluoma.quit-dropbox-at-time.plist' '/Library/LaunchAgents'
  1. Then you need to make sure that the files are owned by root:wheel or else launchd will ignore them:
sudo chown 'root:wheel' '/Library/LaunchAgents/com.tjluoma.launch-dropbox-at-time.plist'

sudo chown 'root:wheel' '/Library/LaunchAgents/com.tjluoma.quit-dropbox-at-time.plist'
  1. Just for good measure, make sure they have reasonable permissions:
sudo chmod 644 '/Library/LaunchAgents/com.tjluoma.launch-dropbox-at-time.plist'

sudo chmod 644 '/Library/LaunchAgents/com.tjluoma.quit-dropbox-at-time.plist'
  1. Log out and/or reboot the Mac.

Repeat steps 1-5 for each of the 8 Macs.

Tada!

That should do it. Obvious caveats abound: I’m just a normal person and can make mistakes, use at your own risk, etc. But as far as I know, this should do exactly what you are looking for.

Let us know how it goes!

2 Likes

Except that they want the computers to sync at night… which will be much harder to do if they are turned off :smiley:

I think that the OP means that Dropbox uses significant resources on the iMacs rather than network slowness/bandwidth causing the issue.

I agree that a network server or cloud provider would be a better solution than Dropbox

Thank you very much for this detailed solution, I’ll go righ ahead an try it! Thanks again!

If possible, a networked NAS or something else local does sound like it would work better, but knowing the realities of many places, that may not be feasible for any number of reasons, from technical to financial to ‘political’.

1 Like