Tuesday, September 18th, 2007...10:18 am
iTerm for the lazy
Jump to Comments
Here’s the problem:
I have to connect to my business’s server throughout the day. In fact, ideally, I want to stay connected all the time. I have to be connected to:
- Run SQL to resolve support queries, analyse trends, fix issues
- Deploy new code
- Monitor log files to see if anything ‘bad’ is happening
- Monitor the server processes to see how well it’s coping and whether we need to start thinking about an upgrade
- Run shell scripts for a whole variety of other purposes
Basically, I’m on there all the time. Most of the time, I use 4 types of sessions: a mysql console, a log file tail, a top view of the processes, and a series of miscellaneous bash shells to do other things.
I could keep all these things open simultaneously thanks to screen. And to be fair, screen is a great utility. However, I’ve gotten used to the idea of having tabs in iTerm, on my Macbook Pro. It’s nice to be able to Cmd-Left and -Right to navigate between the different sessions you have open, and Ctrl-A is such an awkward key combination to have to type all the time. I also like the way the iTerm tabs light up with activity when something happens on one tab.
To add insult to injury, I so far have failed repeatedly in getting the keyboard mappings to work through the combination of iTerm + SSH + screen (although they work fine with iTerm + screen). A little while ago, I decided to sort this out once and for all.
My solution involved ditching screen and using a smaller utility called dtach instead. dtach doesn’t mess with your keyboard. All it does is keep sessions open so you can resume them if you disconnect for any reason. So I installed that on my server (It’s a debian server, so that’s a simple sudo apt-get install dtach).
I then created 8 little scripts called src1.sh to scr8.sh, and in each I put the command to create a dtach session containing what I wanted. For instance, in my first script, it says:
dtach -A /tmp/sess_screen1 mysql -u <user> -p<password> <dbname>
In my second one:
dtach -A /tmp/sess_screen2 sudo tail -f /var/log/messages
And so on and so forth. So that’s step 1 - keeping the sessions open and making sure they contain what I want.
The second step is figuring out how to open all of those automatically without me having to type those script names manually in the terminal.
iTerm could allow this directly, since I could attach a different profile to 8 different key combinations, and to launch them I would just press, for instance, Cmd-Ctrl and then hit A, S, D, F, G, H, J, K. That would work, but it’s a bit crude. I’d rather type one single command and have all these open at the same time without any further input.
This is where AppleScript comes in. Now, before I did this I’d never used AppleScript, but it is renowned for being ridiculously simple. I have to say it lived up to its reputation. You can edit it with a Mac OS app called Script Editor (find it with Quicksilver). Then, put the following script in the box:
A couple of prerequisites here:
- The bookmark is called “server1”. If your bookmark is named differently, change the string in the “launch session” bit
- I’ve of course set up ssh keys so I don’t have to type in the password on each session
If you save the app in your Applications folder (I saved it in my user’s Applications), you can then access it with Quicksilver once it refreshes its application cache.
Now, launching my eight tabs into a new window takes only: Cmd-Space, “srv1”, Enter. That’s it. I’m logged in.
As an additional nicety I’ve made the background colour for “server1” red in the iTerm profiles, since it’s our production server and I want to recognise when I’m on it and not make any snafus. The one tiny bothersome thing that I haven’t managed to resolve is making the title of the tabs stick. As soon as the ssh session goes through, iTerm all-too-cleverly decides that it’s wise to rename the tab to “username@server1…”, which is not particularly helpful. But it’s good enough, and a long way away from the days when my session log-on consisted of creating a bunch of tabs, typing my password in each of them, and then launching the commands I wanted in each - until the next time I put my laptop in sleep mode…
I hope people find this useful. If you do - especially if you happen to know how to change the title names - feel free to comment below!
Leave a Reply