Archive for November, 2009

Find out which application is using external hard drive in order to eject it

Monday, November 23rd, 2009

When using the Mac OS X UI to eject an external hard drive, I occasionally get this error: ‘The disk “[volume name]” is in use and could not be ejected. \ Try quitting applications and try again.” Only mac won’t tell me which applications are using this drive. I hopelessly try to kill applications by force quitting or even using activity monitor to zap processes.

I think I have finally solved this.

Open up the program called Terminal.app (located in Applications/Utilities). Enter the following replacing [name of drive] with the name of your hard drive:


sudo lsof +D "/Volumes/[name of drive]"

You should get a list that looks something like this:


COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
bash      637  ajx  cwd    DIR   14,2      442 2993107 /Volumes/path/to/file/in/use
preview   638  ajx  cwd    DIR   14,2      442 2993107 /Volumes/path/to/file/in/use
...

These are the applications/processes that are using your hard drive. They must be killed (quit) before you eject it safely.

Try to quit any applications you recognize the normal way. For each of the other processes (line items) you want to issue the kill command followed by that process’s PID. You might get errors in which case for each PID try the following until you don’t get an error. You can always run the lsof command again to see if the process really died.


kill [PID of process]
sudo kill [PID of process]
sudo kill -INT [PID of process]
sudo kill -KILL [PID of process]

Update: So it seems sometimes lsof will just hang… I don’t know what to do in this case, hopefully I will update again with further solutions.

Synergy server and client settings and commands

Saturday, November 21st, 2009

After the recommendation of a linux friend, I have begun using synergy to share my mouse and keyboard between the new macbook pro on my desk and the old powerbook two shelves up (I do have to physically switch the DVI cable to see the right computer on my monitor though … DVI switches still cost too much).

On the server (the computer which actually owns the mouse and keyboard), named
Enfermera:

Here is my synergy.conf file saved in ~/.synergy.conf


section: screens
  Enfermera.local:
  AJX.local:
end
section: links
  Enfermera.local:
    right = AJX.local
  AJX.local:
    left = Enfermera.local
end

Then on this computer (server) I run the command


synergys -f --config ~/.synergy.conf 

On the client (the computer not physically or emotionally connected to the mouse and keyboard), named AJX:
I run the command to start listening for mouse and keyboard coming from Enfermera:


synergyc -f Enfermera.local

Note: Another awesome feature of synergy is that your linked computers will share clipboards allowing you to copy from one computer and paste into the other.

Note: I was able to install this on Mac OS X 10.4 and Mac OS X 10.5 (both using sudo port install synergy), the two operating systems had no problems talking to each other through synergy.

Test ssh connection speed

Thursday, November 19th, 2009

Having a few different ssh hosts these days it’s become convenient to be able to test my upload and download transfer rates. Here’s my SSH speed test bash script. It uses dd to generate a test file then uses scp to test the transfer rates across the network.

#!/bin/bash
# scp-speed-test.sh
# Author: Alec Jacobson alecjacobsonATgmailDOTcom
#
# Test ssh connection speed by uploading and then downloading a 10000kB test
# file (optionally user-specified size)
#
# Usage:
#   ./scp-speed-test.sh user@hostname [test file size in kBs]
#

ssh_server=$1
test_file=".scp-test-file"

# Optional: user specified test file size in kBs
if test -z "$2"
then
  # default size is 10kB ~ 10mB
  test_size="10000"
else
  test_size=$2
fi


# generate a 10000kB file of all zeros
echo "Generating $test_size kB test file..."
`dd if=/dev/zero of=$test_file bs=$(echo "$test_size*1024" | bc) \
  count=1 &> /dev/null`

# upload test
echo "Testing upload to $ssh_server..."
up_speed=`scp -v $test_file $ssh_server:$test_file 2>&1 | \
  grep "Bytes per second" | \
  sed "s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\1/g"`
up_speed=`echo "($up_speed*0.0009765625*100.0+0.5)/1*0.01" | bc`

# download test
echo "Testing download from $ssh_server..."
down_speed=`scp -v $ssh_server:$test_file $test_file 2>&1 | \
  grep "Bytes per second" | \
  sed "s/^[^0-9]*\([0-9.]*\)[^0-9]*\([0-9.]*\).*$/\2/g"`
down_speed=`echo "($down_speed*0.0009765625*100.0+0.5)/1*0.01" | bc`

# clean up
echo "Removing test file on $ssh_server..."
`ssh $ssh_server "rm $test_file"`
echo "Removing test file locally..."
`rm $test_file`

# print result
echo ""
echo "Upload speed:   $up_speed kB/s"
echo "Download speed: $down_speed kB/s"

Note: I’m still looking for a good way to test general internet upload and download speed via the command line… Save it in a file called scp-speed-test.sh. Then run it with the command:

bash scp-speed-test.sh user@hostname

Back up user directory using rsync over ssh as hourly cron job

Wednesday, November 18th, 2009

First I followed this blogger’s instructions to set up my ssh password so that my script wouldn’t have to prompt for a password. I tested this by sshing into my destination server (shouldn’t ask for password now). There I made a directory to put my backup:


mkdir ~/macbook-pro-backup

I don’t want to back up everything in my user directory so I made an exclude file at ~/.cims-exclude-list:


/ajx/Library
/ajx/Music
/ajx/Movies
/ajx/Pictures
/ajx/Documents/Parallels
/ajx/Downloads
/ajx/Desktop
/ajx/.Trash
*.mp3
.DS_Store

Then I constructed and tested my rsync command:


rsync -e ssh -avz --exclude-from ~/.cims-exclude-list ~ jacobson@access.cims.nyu.edu:macbook-pro-backup

Note: I have not used the --delete option because for now I have the space available on my destination server, but eventually I will probably have to start deleting older, no-longer-existent files.

After I used the above command to do the initial (big/long) transfer, knowing that it works I constructed this bash script to keep a nice log. I saved this in cims-backup.sh:


#!/bin/bash
touch ~/.cims-backup.log
if rsync -e ssh -az --exclude-from ~/.cims-exclude-list ~ \
  jacobson@access.cims.nyu.edu:macbook-pro-backup
then
  echo "Back up succeeded on $(date)" >> ~/.cims-backup.log
else
  echo "Back up failed on $(date)" >> ~/.cims-backup.log
fi

Issue the following command to make this file executable


chmod -x cims-backup.sh

I ran this (bash cims-backup.sh) a couple times and checked the log file: cat ~/.cims-backup.log should produce something like


Back up succeeded on Wed Nov 18 16:23:49 EST 2009
Back up succeeded on Wed Nov 18 16:24:04 EST 2009

To be sure I sshed into my destination server and checked the macbook-pro-backup directory.

All that’s left is to set up an hourly cron job. To do this, in the terminal issue:


crontab -e

Then add this line and save:


0 * * * * /path/to/cims-backup.sh

If you are using vim to edit and you are getting this error:


crontab: temp file must be edited in place

then add this line to your .vimrc file:


set backupskip=/tmp/*,/private/tmp/*

Size of current directory using command line terminal

Wednesday, November 18th, 2009

This is all over the internet, put to repost it here for redundancy and as a reference for myself. Here’s how to print the size in a human readable format (-h) of just the current directory (-s .) using du:


du -sh .

New erutnevdAtxeT story arcs added

Wednesday, November 18th, 2009


new erutnevdatxet story arcs

I’ve added some new story arcs to erutnevdAtxeT, the backwards text adventure game I’ve posted recently. Check it out and see if you can get to the beginning of the game. Or try to find all different causality pasts. I’ve added a “refresh” or “reveal the future you just chose feature” (just hold SPACE) to help keep track of your current arc as you play.

Still hoping to convert this to an in browser silverlight app sometime…

PyQt’s magical disappearing (but not reappearing) windows on Mac OS X

Monday, November 16th, 2009

After the grueling Qt and PyQt installation process on my Mac OS X 10.5 machine, I have PyQt up and running. I made a little hello, window type app for a sanity test and found a very puzzling feature. PyQt can make a resizable window which the mouse can resize to nothing! This is not allowed through the API methods (namely widget.resize(...,...)), with those the window always keeps a minimum width and height. But the user is allowed to drag the window to nothingness. If the user lets go while the window has non-positive dimensions then the window is lost forever as far as the mouse user is concerned.

Here’s the simple PyQt code:


import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.show()
sys.exit(app.exec_())

And here are some screenshots of the result:

pyqt-disappearing-window-01.png


pyqt-disappearing-window-02.png


pyqt-disappearing-window-03.png


pyqt-disappearing-window-04.png


pyqt-disappearing-window-05.png


pyqt-disappearing-window-06.png


pyqt-disappearing-window-07.png


pyqt-disappearing-window-08.png

Update:
Here’s an animated gif version illustrating the above:
pyqt disappearing window animated gif

Update:
Shucks! Seems like this is fixed in the new version of PyQt4 that I just got.

USB powered laptop charger

Sunday, November 15th, 2009

Ever wish you could charge your laptop, but there’s no outlet around. Now you can charge your Macbook or Macbook Pro using its own USB port. Below is a photo of the prototype for a USB powered magsafe laptop power adapter. Just plug one end into your laptop’s USB port and plug the other into the power socket.
usb magsafe laptop charger

Open lines in clipboard as URLs in Safari

Saturday, November 14th, 2009

I used to use a script that took the lines of a text edit document and opened each as a url in Safari. This was useful when I used text-edit. Now I’m using Terminal and other apps to gather urls so its easier and more general to just copy the urls to the clipboard (CMD + C) and run this script. The applescript below opens each line of the copied text as a new safari window setting the line as the url.


set clipboard_contents to the clipboard as text
set urlList to paragraphs of clipboard_contents
set numURLs to (count urlList)
tell application "Safari"
	activate
	repeat with this_url in urlList
		if length of this_url is greater than 0 then
			set this_doc to make new document at end of documents
			set URL of this_doc to this_url
		end if
	end repeat	
end tell

Note: There are lots of hacks to open a list of urls as tabs, find one and use it if your desire tabs. I didn’t post this for tabs because as far as I know opening a bunch of tabs always requires and awkward hack like using system events and keystrokes etc.

Confusing Math Letters Examined

Thursday, November 12th, 2009

I once looked down at my Abstract Algebra notes and noticed how many letters of my hand written formulas and equations looked eerily alike. I wrote out a bunch of the really similar ones and was shocked by the long spectrum they spanned. I have elaborated that now. In doing so realizing that these letters and their similarities belong in a higher dimensional space, at least to really show their proximity. However, it is still see them lined up in one dimension.
math letters written by hand
117Zz2zηζξδ∂2ασadq9φωwεE8θΦØ0Θ☉o⚬•⋅

I want to make an animation that twists and stretches a single curve into each of these sequentially.