Home > code > Tailing Multiple Logs with Cygwin

Tailing Multiple Logs with Cygwin

I just joined a new project at work building a workflow system using the Documentum successor Alfresco and its Business Process Management engine Activiti. When the project dev lead gave me a tour of developing for and using the system, I noticed that he opened Cygwin bash shells to tail three different Alfresco log files to look for errors. If you’re not of the software dev persuasion but are trying to make sense of this post for some reason, let me try to explain. Tail is a standard UNIX tool to show the end of a file from the command prompt, and using the -f option dynamically updates the display when new stuff gets added to the file. Thus tailing logs allows you to see the under-the-hood output from your software so you can figure out what the heck is really happening.

I realized that I would lose my mind tailing 3 different log files in 3 different Cygwin shells constantly, and immediately set out to make this process easier.

The first problem is that Alfresco makes new log files for each day, and uses the date in the filename. Thus the tail command needs the smarts to tail today’s log using the date. Here’s my bash script to tail Alfresco’s stdout log.

#!/bin/bash
today=`date +%Y-%m-%d`
tail --lines=200 -f /cygdrive/c/Alfresco/tomcat/logs/alfrescotomcat-stdout.$today.log

For this I want to run a single Windows batch file that kicks off 3 separate Cygwin shells for tailing the logs. That batch file is simple:

start /d "C:\cygwin64" Cygwin-tail-localhost.bat
start /d "C:\cygwin64" Cygwin-tail-stdout.bat
start /d "C:\cygwin64" Cygwin-tail-stderr.bat

Now comes the slightly tricky part, the batch files that kick off the Cygwin shells. Here’s what the standard Cygwin.bat script looks like:

@echo off
C:
chdir C:\cygwin64\bin
bash --login -i

I tried appending to the above bash command to run my ~/.tail-*.sh script, but no matter what I tried I could not customize the title of the window. This gave me 3 windows that looked the same and I couldn’t tell which window displayed which log file. So I abandoned the bash command and switched to Google’s mintty terminal emulator, and everything came together nicely. Here’s what Cygwin-tail-stdout.bat looks like:

@echo off
C:
chdir C:\cygwin64\bin
start mintty.exe -s 120,60 -t stdout /bin/bash -l -e './tail-stdout.sh'

Now I click on one icon in my Start menu and instantly 3 bash consoles appear tailing Alfresco’s logs. There are probably smarter ways to do this, but I’m pretty happy with this approach.

Categories: code Tags: , ,
  1. September 25, 2013 at 10:08 am

    Have a look at multitail: “MultiTail allows you to monitor logfiles and command output in multiple windows in a terminal, colorize, filter and merge”.

  1. No trackbacks yet.

Leave a comment