› Reconnecting iTerm ssh Sessions on Wake
If you spend lots of time connected to various remote systems via ssh then putting your computer to sleep can be quite annoying. Once you wake it back up, you have to go to each iTerm tab that contains a remote ssh session and close it (or wait for it to eventually time-out) then open a new one. The following script, run via SleepWatcher, will do this for you each time you wake your Mac from sleep.
Make sure you read to the end, there is a second method that is a bit less work to setup.
› The Script
Apple script to run on wake:
set TargetSession to "orion"
tell application "iTerm"
activate
set TerminalList to terminals
repeat with myTerminal in TerminalList
set SessionList to sessions of myTerminal
repeat with mySession in SessionList
tell mySession
if name = TargetSession then
terminate
end if
end tell
end repeat
end repeat
tell the first terminal
launch session TargetSession
tell the last session
write text "screen -xd"
end tell
end tell
end tellCopy and paste this code into a new Script Editor window. You will need to change the TargetSession from "orion" to whatever you have named the bookmark you want to close and re-open on wake. This is case-sensitive, if the case is not correct you will get the odd behavior of your target session closing but not relaunching.
Once you have pasted the script and set the target name, save the script as an application in script editor. I have a directory named "bin" within my home directory for little things like this.
› Installing SleepWatcher
Grab the SleepWatcher package from http://www.bernhard-baehr.de/. The current version (2.0.4) has two packages in the DMG. "sleepwatcher" and "SleepWatcher StartupItem" install both in this order. The second sets SleepWatcher to launch on boot and sets it up to look for a file named .wakeup and .sleep in the user's home directory on wake and sleep.
› Making it Run
SleepWatcher is setup to run ~/.wakeup when the system wakes from sleep so now we just need to create a script with this name and have it call our applescript. One gotcha I had with my MacBook that wasn't a problem on my iBook was the time needed to reconnect to the wifi network on resume from sleep. To solve this, I have written the following shell script to check if the network is available before running the applescript.
#!/bin/bash
counter=0
limit=20
while [ "$counter" -lt "$limit" ]
do
curl http://www.google.com/ &> /dev/null
if [[ $? = 0 ]]; then
open ~/bin/reconnect\ iterm.app
exit
fi
count=$(($count+1))
sleep 0.5
done
This script will attempt to retrieve http://www.google.com/, if successful it will run the applescript and exit. If it fails, it will wait half a second and try again, up to 20 times (about 10 seconds). Somewhere between 1 and 3 seconds was usually the length of time it took my MacBook to reconnect to my WPA2 WIFI network.
You will need to modify line 10 to point to the location you saved the applescript earlier. Save this script as ".wakeup" in your home directory so that SleepWatcher will find it. Once saved, simply set the script to executable "chmod 700 ~/.wakeup" from a shell prompt and you are all set.
› An Alternative .wakeup Script
#!/bin/bash
TARGET_SESSION=orion
counter=0
limit=20
DISCON=$(cat << EOC
tell application "iTerm"
activate
set TerminalList to terminals
repeat with myTerminal in TerminalList
set SessionList to sessions of myTerminal
repeat with mySession in SessionList
tell mySession
if name = "$TARGET_SESSION" then
terminate
end if
end tell
end repeat
end repeat
end tell
EOC);
RECON=$(cat << EOC
tell application "iTerm"
tell the first terminal
launch session "$TARGET_SESSION"
tell the last session
write text "screen -xd"
end tell
end tell
end tell
EOC);
echo "$DISCON" | osascript -
while [ "$counter" -lt "$limit" ]
do
curl http://www.google.com/ &> /dev/null
if [[ $? = 0 ]]; then
echo "$RECON" | osascript -
exit
fi
count=$(($count+1))
sleep 0.5
done
This alternate .wakeup script includes the AppleScript. The advantage of this method is ease of installation as once Sleep Watcher is installed you only have one file to create. The configuration variables are at the top. Another benefit is it can close the old tab immediately and then wait for the network connection to come up before reopening the tab, rather then waiting before even closing the old tab. Thanks to Dan Christensen and Donnie Berkholz for helping me make AppleScript and bash come together in perfect harmony, despite my piddly small knowledge of bash scripting and Seemant Kulleen for pointing out some typos.

