May 18th, 2007

How to tail files in Windows

I’m a big fan of cygwin, so most of the time I have countless small-but-useful utilities at my disposal, without having to think twice. But, when doing system administration on a client’s server in an off-site data center, I’m very limited in what software I’m allowed to put on the machine. For sysadmin tasks, though, unix utilities are pretty hard to do without.

For instance, one of my favorite tools for watching log files is tail, usually with a -f or -F parameter (”follow” and “follow+retry”, respectively; both let you see new lines as the file(s) grow, the latter keeps trying in case the file doesn’t exist yet). Unfortunately, vanilla Windows installations still don’t come with a tail utility. So, I went searching for other ways of tailing files, besides installing cygwin.

One method that I found can be used without any additional software. It’s just a batch file. It can’t follow changes, but you can at least view the last N lines of a file. Here it is, from a FAQ on Microsoft TechNet, by Jerold Schulman:

@echo off
if {%1}=={} @echo FileName parameter required.&goto :EOF
if not exist %1 @echo %1 does NOT exist.&goto :EOF
setlocal
set file=%1
set /a number=10
if not {%2}=={} set /a number=%2
for /f %%i in ('find /v /c "" ^< %file%') do set /a lines=%%i
@echo %lines% lines in file %file%.
if %number% GEQ %lines% set /a start=0&goto console
set /a start=%lines% - %number%
:console
more /e +%start% %file%
endlocal

I really missed tail -F, though, so I went looking again for some way to tail -F without cygwin. There are plenty of freely available utilities out there that should do the trick, but I wanted to find the simplest, safest one, from a trustworthy source, preferrably open-source so the integrity is verifyable. After looking at a couple open-source options, I stumbled across a page on malektips.com: Windows XP and DOS - Unix Style Tail Command

I learned from that page that you can download the Windows Server 2003 Resource Kit Tools package for free, and that it includes a tail utility (including -f functionality). After downloading and installing that on my laptop, I looked in the installation directory, and found tail.exe there. It’s only 7k!

Next, I FTP’d that to the server I needed to run it on, and gave it a try, hoping that it did not have dependencies that required installing the whole Resource Kit Tools package. As it turns out, it worked liked a charm! It’s nice and small, and arguably from a trustworthy source (Microsoft itself). Easier to justify than most of the other tools I was able to find, in case anyone ever questions my putting it on the server.

August 23rd, 2006

Batch file: Trillian global disconnect and reconnect

I use Trillian for most of my instant messaging, because it allows me to connect to AIM and Yahoo through one common user interface (it also supports ICQ, MSN, and IRC, but I don’t use [it for] those). The features I like about it are numerous, but one of my major complaints is the way it sometimes has trouble reconnecting after being disconnected from one of the networks.

Normally, it tries a few times to reconnect, automatically. But sometimes it seems like it gives up too soon. When this happens, you have to right click the system tray icon, go to the “Connections” submenu, and choose “Global Disconnect”. Then, you have to do the exact same thing, but choose “Global Reconnect”. The initial “Global Disconnect” is usually required, because Trillian is stuck in a state where it thinks you are connected in some way, such that just doing “Global Reconnect” wouldn’t do anything.

As anyone that knows me could tell you, I hate doing extra steps, particularly involving the mouse, when a simple keyboard shortcut could suffice. So, several years ago, I made this simple batch file, which doesn’t do anything magical, but I use it almost every day.

The batch file, included in the box below, uses TrillKey to send a “Global Disconnect” command to the currently-running instance of Trillian. Then, it waits 1 second, to give Trillian time to deal with the first request. Then, it uses TrillKey to send a “Global Reconnect” command to Trillian. For the brief delay in between commends, I use the ’sleep’ command from cygwin, but a couple other ways to put delays into batch files can be found here: http://malektips.com/xp_dos_0002.html.
To call this batch file, I have a shortcut to it on my desktop, with a shortcut key assigned to it (Ctrl-Alt-T). Works like a charm!

@echo off
c:\programs\trillkey.exe disconnect
sleep 1
c:\programs\trillkey.exe reconnect

While writing this post, I noticed that I’m using a really old version of TrillKey (from 2002). The latest version includes additional features, so my batch file can be simplified to a single command:

@c:\programs\trillkey.exe disconnect delay 1 reconnect

TrillKey can do a LOT more than just disconnect and reconnect, though. I highly recommend checking it out.