TAIL a useful DEBUGGING command in UNIX
The tail command displays the last 10 lines of a file on standard output (usually the terminal). The number can be modified by using -nn, where nn is the number of lines to display. For example tail -20 log.txt will display the last 20 lines of the file log.txt.
If a + is used, the number of lines specified is from the beginning of the file. For example tail +20 log.txt will skip lines 1 through 19 of log.txt and display lines 20 through to the end of the file.
The tail utility is particularly useful for looking at the last lines logged in a log file. It also has a very useful option called the -f flag. If tail is used with a -f flag, the file being displayed is not closed, but is kept open. The tail program sleeps for one second, wakes, and checks to see if more lines have been added to the file. If they have, the new lines are displayed. This option is particularly useful for monitoring a log file that is currently active and being written to.
Assuming that log.txt is a logging file being written to by one or more programs, tail -f log.txt will display the last 10 lines of log.txt, and then update the screen each second with any new records added to log.txt by other programs.
tail -f is an excellent debugging tool. The first time I used it was to monitor a file that was being filled with transactions by cash registers as cashiers rang up purchases. For testing purposes, we set up the register, rang up different types of transactions, and then examined the result on the Unix screen, immediately. In this way we were quickly able to isolate transactions that were being written to the file incorrectly.
Because tail routes its output to standard output, it is possible to pipe the results of tail into another process. At one point in the debugging, we were concerned that the cash registers were writing garbage or nulls to the transaction file. tail processed the files as text, and it was not possible to see nulls in the data. We used tail to pump the data into od (octal dump) and displayed the files in hex, so that we could examine them for nulls. Using tail -f at the beginning of the stream meant that the transaction file was kept open and constantly pumped into od where the bytes were translated into hex and displayed.
Here is the command:
tail -f trx.txt|od -xc
Testing tail
You might want to try the following to test tail. First we will create a process that writes to a log file. Start the Korn shell by typing ksh and pressing Enter.
Type each of the following lines. As you press Enter after each line, the > prompt will appear indicating that more input is expected.
The command listed below creates a process that sleeps for two seconds and then awakens and appends the date and time to log.txt. The process is submitted to the background by enclosing all of the commands in parentheses at lines 1 and 6, and a final ampersand (&) . When you press Enter at the last line, the process is submitted to the background and runs as a detached job with no terminal to write to. It doesn't need a terminal since it is writing to log.txt.
After submitting the job in background, the operating system responds by giving you the job number of the job that is now running in the background. Make a note of the process id number that appears on your terminal, as you will need it later to kill the process. In this example it is 18495.
$ (while true
> do
> sleep 3
> echo `date` >>log.txt
> done
> )&
18495
Now that log.txt is being filled with date and time stamps, enter the following command:
tail -f log.txt
As you watch the screen, log.txt is filled with more information, and tail -f continues to display the information on the screen as in the following example.
$ tail -f log.txt
Tue Jun 7 12:33:14 GMT 2005
Tue Jun 7 12:33:17 GMT 2005
Tue Jun 7 12:33:20 GMT 2005
Tue Jun 7 12:33:23 GMT 2005
Tue Jun 7 12:33:26 GMT 2005
Tue Jun 7 12:33:29 GMT 2005
Tue Jun 7 12:33:32 GMT 2005
Tue Jun 7 12:33:35 GMT 2005
Tue Jun 7 12:33:38 GMT 2005
Tue Jun 7 12:33:41 GMT 2005
Tue Jun 7 12:33:44 GMT 2005
Tue Jun 7 12:33:47 GMT 2005
Press Control-C or the Delete key to stop your tail -f process depending on how your terminal is set up.
Finally you need to stop the background process that is logging to the log.txt file. Using the process id number you noted after the process started type:
$kill 18495
Labels: Technical_HowTo_Unix
0 Comments:
Post a Comment
<< Home