Killing processes in OSX while tethered via WiFi

When I’m at work I tether my laptop to my mobile phone. Only having 1.5GB of data per month to use, this can get eaten up quickly with Dropbox and or SugarSync working, not to mention other things that use the data.

Dropbox has a manual “pause” button, but SugarSync doesn’t. Since I’ve changed it’s even harder to remember that things are syncing, so I looked for a way to kill it while I’m tethered.

It ended up being easiest to setup a script which runs through crontab.

It works in the following way:

  • Uses the _airport_ command to check which AP I’m connected to.
  • Checks if it matches the AP my phone offers (if not then it’ll stop).
  • Check if SugarSync is running, then kill it.
  • Check if Dropbox is running, then kill it.

When it’s going to kill something, it’ll use _logger_ to write a syslog entry.

I saved it as _killifmobile.sh_ and chmod +x’d it so it’ll run as a program. The /1 means it runs every minute. I added it to crontab with the following line:

 */1 * * * * killifmobile.sh 

The code for the program is below:

 #!/bin/bash 
 function debug_log() { 
 logger -t "KillIfMobile" -i 
 } 

 export AP=AP 
 CURRAP=`/usr/sbin/airport -I | grep " SSID" | awk '{ print $2 }'`
 if [ "$CURRAP" == "$AP" ] 
     then
     if [ `ps aux | grep SugarSync | grep -v grep | wc -l` -gt 0 ] 
     then 
     debug_log "Killing SugarSync..." 
     kill `ps aux | grep SugarSync | grep -v grep | awk '{ print $2 }'` 
     fi 

     if [ `ps aux | grep Dropbox | grep -v Dropbox | wc -l` -gt 0 ] 
     then
     debug_log "Killing Dropbox..." 
     kill `ps aux | grep Dropbox | grep -v grep | awk '{ print $2 }'` 
     fi
 fi


#bash #OSX #pause sugarsync #script #tethered #Wireless