Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Display realtime log files automatically UNIX
One of the tools I use for application development creates a new log file in a particular directory every time it is started. Normally, if I want to keep watching what's happening in the log, I have to do a tail -100f logfile_name in the Terminal. Attach this AppleScript to your log folder, and it will automatically open a terminal window "tailing" the logfile whenever a new one is created. Please note that this is my very first AppleScript, so there are probably better ways to do it. Have fun!
on adding folder items to this_folder after receiving these_items
  try
    repeat with i from 1 to number of items in these_items
      set this_item to item i of these_items
      
      tell application "Finder"
        set theName to the name of this_item as string
      end tell
      
      tell application "Terminal"
        activate
        do script with command ¬
         "/usr/bin/tail -100f '/path/to/your/logfiles/" & ¬
         theName & "';exit"
      end tell
    end repeat
  on error error_message number error_number
    if the error_number is not -128 then
      tell application "Finder"
        activate
        display dialog error_message buttons {"Cancel"} ¬
         default button 1 giving up after 120
      end tell
    end if
  end try
end adding folder items to
[robg adds: I haven't tested this, beyond making sure the script compiles, which it does.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[6,347 views]  

Display realtime log files automatically | 10 comments | Create New Account
Click here to return to the 'Display realtime log files automatically' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
To you AppleScript wizards:
Authored by: jecwobble on May 26, '04 10:51:06AM
Could you replace the hard coded path "/path/to/your/logfiles/" with the this_folder variable? If so, would that allow you to attach this script to any folder as is, without needing to modify the hard coded path- a one script, many folders type of thing?

[ Reply to This | # ]
To you AppleScript wizards:
Authored by: nhajratw on May 26, '04 11:10:13AM

I'm sure there is a way to do it... i don't think this_folder works though, since it has to be a unix style path.

I think it can be done with an applescript "POSIX" keyword or something...

Any AppleScript gurus want to help out?



[ Reply to This | # ]
Here's one solution
Authored by: saint.duo on May 26, '04 12:00:48PM
IF /path/to/your/logfiles/ is the same folder as this_folder, then changing this should work:

tell application "Terminal"
        activate
        do script with command ¬
         "/usr/bin/tail -100f '" & (quoted form of POSIX path of this_folder) & ¬
         theName & "';exit"
end tell
I hope I cleaned up the quotes properly. I'm not familiar with the tail command. There is a space and a single quote between -100f and the quotation mark.

---
--
duo

[ Reply to This | # ]

Here's one solution - quotes in wrong place
Authored by: Krioni on May 27, '04 10:34:26AM
Your quotes are in the wrong place - you have to combine the two strings BEFORE you quote them, otherwise you'd get 'something''somethingelse' which will not work.

tell application "Terminal"
	activate
	do script with command ¬
		"tail -100f " & (quoted form of ((POSIX path of this_folder) & theName)) & ";exit"
end tell
So, we combine the Posix path of this_folder with theName FIRST, then we get the quoted form of it. Note that also means you do NOT include the single quotes yourself - that's what "quoted form of" does.

Also, you don't need to specify where "tail" is by using the full path, unless you're afraid the user may have another version of tail that you don't want to use.

[ Reply to This | # ]

Display realtime log files automatically
Authored by: lpp on May 26, '04 12:47:01PM
Here's the same thing, but targetting the Console.app which is in /Applications/Utilities:

on adding folder items to this_folder after receiving these_items
	try
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			
			tell application "Finder"
				set theName to the name of this_item as string
			end tell
			
			tell application "Console"
				activate
				open (quoted form of POSIX path of this_folder) & theName
			end tell
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			tell application "Finder"
				activate
				display dialog error_message buttons {"Cancel"} ¬
					default button 1 giving up after 120
			end tell
		end if
	end try
end adding folder items to
I included the POSIX path conversion bit

[ Reply to This | # ]
Display realtime log files automatically
Authored by: jbc on May 26, '04 02:53:28PM

You might also be able to incorporate something like the setup I use for MkConsole. It's pretty flexible, so you could probably adapt the method to work with Terminal or Console somehow.

A shell script set as a login item -F tails all files in a folder of symlinks to log files I want to monitor (this works with GNU tail; not sure about the default tail). This gets redirected through a sed filter into a single log file that you can monitor for all the logs. That way you have only one log file to watch, and it follows new files when they're rotated. etc. Think it will automatically pick up a new file when it appears at the location referenced by the symlink.

#!/bin/sh
#login shell script
echo "" > /Library/Logs/monitor.log
GTAIL=/opt/local/bin/gtail
SED=/opt/local/bin/sed
$GTAIL -n 25 -F /Library/Logs/Monitor/* | $SED -u -f /Library/Logs/logfilter >> /Library/Logs/monitor.log

With the sed filter you can also reformat the main log file to make it more readable. Mine ends up looking something like this:

exim        2004-05-26 10:13:14 Start queue run: pid=328 -qqf
exim        2004-05-26 10:13:14 End queue run: pid=328 -qqf
system      May 26 10:13:14 asx ConsoleMessage: Starting network time synchronization
system      May 26 10:13:14 asx ntpdate[342]: ntpdate 4.1.1@1.786 Fri Sep 12 18:30:10 PDT 2003 (1)
system      May 26 10:13:14 asx set-hostname[352]: setting hostname to asx.local
console     May 26 10:13:16 asx automount[384]: automount version 57
system      May 26 10:13:15 asx ntpdate[342]: step time server 17.254.0.26 offset -0.301559 sec
system      May 26 10:13:15 asx ntpd[357]: ntpd 4.1.1@1.786 Fri Sep 12 18:30:03 PDT 2003 (1)
system      May 26 10:13:15 asx ntpd[357]: precision = 7 usec
system      May 26 10:13:16 asx ConsoleMessage: Starting network file system
exim        2004-05-26 10:14:13 HYBZVM-0000AV-5C <= ann5120-errors+1027853.896030489 ...
            P=esmtp S=34040 id=5.600.5.0.0.28327.1085589935@sender20.mail2.experian-ems.com
exim     *  2004-05-26 10:14:16 HYBZVM-0000AV-5C => Failed system filter checks - 0.9999990 ...
exim        2004-05-26 10:14:16 HYBZVM-0000AV-5C => cnn <cnn@localhost> R=local_domain T=local_smtp
exim        2004-05-26 10:14:16 HYBZVM-0000AV-5C Completed
cron        May 26 10:15:00 asx CRON[814]: (root) CMD (/opt/local/sbin/anacron -s)
cron        May 26 10:15:00 asx anacron[815]: Anacron 2.3 started on 2004-05-26
cron        May 26 10:15:00 asx anacron[815]: Normal exit (0 jobs run)
console     fetchmail: background fetchmail at 355 awakened.
ipfw     -  May 26 10:15:33 asx kernel: ipfw: 5000 Deny TCP 209.128.151.246:4056 ...

Makes it pretty easy to follow what's going on in a number of logs at once.



[ Reply to This | # ]
Display realtime log files automatically
Authored by: el bid on May 27, '04 03:21:09AM

A very neat generic solution to this is the freeware FileMonitor from P&L, the UK outfit that sells the Mesa spreadsheet for OS X.

You can download FileMonitor from http://www.plsys.co.uk/filemonitor.htm

---
el bid



[ Reply to This | # ]
Display realtime log files automatically
Authored by: nhajratw on May 27, '04 12:54:51PM

I tried this out and it is a pretty neat tool.

However, it doesn't address the problem I have which is that new files are created in the directory with different names.

The tool can monitor a directory, but that feature only tells you when the directory has changed, and doesn't open the log files within the directory.

At least, that's what I was able to determine from some brief poking around.

Thanks!



[ Reply to This | # ]
Console.app does this for you
Authored by: Graff on May 27, '04 04:51:39AM

The Console.app located in /Applications/Utilities/ does this automatically for you in a nice, neat GUI package. Console.app is basically a front-end to the tail shell tool.

Just open the log file from Console.app and it will open in its own window. If you keep it running in the background it will even notify you if the log has been updated. You can also keep Console.app in the Dock and drag a log file onto it to open the log file. If you quit Console.app when you have open log files then the log files will be re-opened and displayed the next time you run it

- Graff



[ Reply to This | # ]
Console.app does this for you
Authored by: nhajratw on May 27, '04 12:49:25PM

Unfortunately, using Console by itself doesn't address the problem of a *new* file being created for whatever reason. It only deals with updating of the file it currently has open.



[ Reply to This | # ]