views:

88

answers:

3

Hi guys,

Long story short... we have multiple servers which we run perflog monitoring on every night. My job is to convert these logs to .csv format and send them to my e-mail.

This bit it already automated via a .sh script an ex-employee wrote.

What I want automated is to run a batch job after the perfmon logging to look at a specific folder and find the latest .blg file and run the sh script on it (the script is called upload) so that I don't have to log in to each server and do it manually.

e.g.

upload myInitials cd /cygdrive/someLocation/logs/$latestFile$.blg

myInitials and the location can be hard-coded... I just wouldn't know how to find the latest file in the folder and automate it all via a batch file.

Any pointers would be very helpful!

@ Jeremy:

Sorry, I probably should have mentioned in my question that the servers are running 2003 and 2008.

I don't think it would be absolutely necessary to register a change notification on the folder - If the log runs from noon till 7 in the morning, the script will run immediately after (you can set a script to run after a perfmon log has finished in log properties) so the log will almost definitely be the latest file in the folder anyway.

Like I said, I already have a .sh file in place to convert to csv and send to my e-mail, I just need to incorporate it into a batch file so that instead of me going to each of the servers and opening up cygwin and typing upload xx /cygdrive/location/logs/xyz.blg, I can have it automated to run straight after the log has finished without me having to RDC into it.

Thanks for the input!

A: 

First, I don't know if it would meet your requirements, but the Windows Task Scheduler 2 in Vista+ is very robust and can trigger an event even based on log entries. However, extraction and parsing of that log entry may require some scripting, and might have concurrency issues, even if that log entry did indicate the last used process. Chances are none of this is helpful, but just throwing it out there.

Programatically, it would be simple as you can register a change notification on a folder. When a change occurs, you go find the latest file. Then launch the batch file to launch your shell script, or whatever your desired sequence may be.

I think cygwin may even support change notification events via scripting, though I'm unsure. I believe there are linux extensions for this, but I may be wrong.

If it were me, I'd just write a little C++ app to do whatever I wanted.. but for you maybe any (or more likely none) of the above helps ;o.

Jeremy Collake
Thanks for the suggestions Jeremy - I updated my original post! (too long to post as comment)
emtunc
+2  A: 

for getting newest file in a directory, ls -1tr | tail -1 should work.

Vardhan Varma
When I type that in cygwin, it provides no output (no errors either)
emtunc
probably you have no files in the current directory !!
Vardhan Varma
I do! I'm in a directory full of blg and csv files and I ran ls -1tr | tail -1 but it doesn't show anything.
emtunc
@emtunc: The command Vardhan suggests should work. Maybe give it another try. You might have to use `ls -a1tr | grep '[.]blg$' | tail -1` if the .blg file names start with a `.` (dot).
Dan Moulding
Okay it worked when I tried it on the other server - strange. Thanks for the tip!
emtunc
+1  A: 

If you have a Shell script and you job is to call the shell script from a windows batch file then this will work.This assumes the cygwin is installed in C:

Contents of start_cyg.bat

@echo off
set PATH=%PATH%:"C:\Cygwin\bin"

rem bash --login -i

bash "/cygdrive/d/cyg.sh"

Contents of cyg.sh

#!/bin/bash

TAIL=`ls -lrt | tail -1`
echo "TAIL:$TAIL"

If you call start_cyg.bat from windows command prompt you can get the output of the cyg.sh in the console

Raghuram
Thank you very much for the examples, very helpful!
emtunc