Tail monitoring log files

Source: Internet
Author: User

The AIL command outputs the end of the file, which, by default, displays the last ten lines of the file. It is often used to dynamically monitor the growth of the tail content of a file, for example to monitor changes in log files. The tail command corresponds to the head command, which is used to display the contents of the file header.

Common parameters

Format: Tail file

Outputs the trailing contents of the specified file files, and outputs the last ten lines of output by default (the final part of files. Print the last ten lines of each FILE to standard output. )

Format: Tail file1 file2 ...

When multiple files are specified, the file name of each file is displayed, and the end of the file is displayed (with more than one file, precede every with a header giving the file name.)

Format: Tail

Format: Tail-

When you do not specify a file, it indicates that the content is read from the standard input, which is usually used behind the pipe line, using the output of the previous command as the input to the tail (with no file, or whenever file is-, read-standard input.)

Format: Tail-n file

Format: tail-n n file

Format: Tail--lines=n

Displays the last n rows of the file, such as tail-20 file, which shows the last 10 lines of the document, which can be used with other parameters. Note that the Italic n of the above three formats is the actual number of rows to display.

Note: Tail-n can display the text content of the last n rows. So is there a way to show the text content that starts with n lines, and the answer is yes.

The Tail-n +4 file represents the contents of the document starting at line 4th. Counting starting from 1.

Format: Tail-f file

Dynamic tracking file growth (output appended data as the file grows), tail will check every second to see if the files Add new content, if the increase is appended to the original output after the display. In this case, however, you must ensure that the file already exists when you execute the tail command.

If you want to terminate the output of tail-f, press CTRL + C to interrupt the tail program. If the output is not interrupted by pressing CTRL + C, the killall tail can be executed on the other terminal forcibly terminated.

Note: When using TAIL-F to monitor file changes, it may not be very good in some cases. For example, when using the log4j log in a Java application, a new log file is generated every 1 hours, the current log output is in LOG4J.LOG, and after one hours, log4j will rename the LOG4J.LOG to log4j.yyyy-mm-dd-hh form. Then tail-f cannot dynamically output the new log content at this time. The tail command itself provides a number of parameters that do not seem to solve the problem perfectly. Finally, we had to write a script ftail.sh to track the log, see the "Linux real-time tracking log4j log file Bash Script-enhanced the TAIL-F function." just now I looked through the tail manual page and found that Tail-f was able to track this type of log. On the other, this demand should have been satisfied by the people of the Linux world already.

Format: Tail-f file

Format: Tail--follow=name--retry File

The function is the same as the Tail-f file, and it is also a change of the dynamic tracking files, unlike when you execute this command, the file does not exist.

The above processing is for text files, the following is the case for binary files.

Format: tail-c n file

Fetch the last n bytes of a file.

Format: Tail-c +n file

Takes the contents of the nth Word of the file. Counting starting from 1.

Using the sample example one output file trailer

Use the SEQ command to output 20 digits to 1.txt, and then try using the tail command.

[email protected] ~]# seq >1.txt
[email protected] ~]# cat 1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[Email protected] ~]# tail 1.txt
11
12
13
14
15
16
17
18
19
20
[Email protected] ~]# tail-3 1.txt
18
19
20
[Email protected] ~]# Tail-n 3 1.txt
18
19
20
[Ro[email protected] ~]# tail--lines=3 1.txt
18
19
20

[Email protected] ~]# tail-n +14 1.txt
14
15
16
17
18
19
20
[Email protected] ~]#

Example two dynamic tracking tomcat output

Dynamically tracks tomcat output.

[Email protected] logs]# tail-f catalina.out
At Org.apache.catalina.connector.CoyoteAdapter.service (coyoteadapter.java:298)
At Org.apache.coyote.http11.Http11Processor.process (http11processor.java:852)
At Org.apache.coyote.http11.http11protocol$http11connectionhandler.process (http11protocol.java:588)
At Org.apache.tomcat.util.net.jioendpoint$worker.run (jioendpoint.java:489)
At Java.lang.Thread.run (thread.java:619)
2010-12-03 13:23:02,236 [http-80-15] DEBUG mhr.roi.mhrmanager-mhrgetjobreq={seq=991,job_id= ' 86130469-0006 '}
2010-12-03 13:23:02,301 [http-80-15] DEBUG mhr.roi.mhrmanager-mhrgetjobrsp={seq=991,result=0 (success), info={job_id= ' 86130469-0006 ', employer_id=86130469,employer_name= Wuxi Fuji Fashion Co., Ltd.,,, job_title= ' |570309| ', job_title0= ' clerk ', job_type=f (Full-time), issue_time= ' 2010-11-03 00:00:00.0 ', work_address= ' 1902 ', work_address0= Wuxi, desired_count= ' 1 ',,,,,, job_desc= Junior College, more than 1 years working experience in 5s management, proficient in computer operation. , required_experience=1 (more than one year),,,, required_degree=15 (college),,,,,,, Valid_days=30,access_count=12,expire_time= ' 2010-12-03 00:00:00.0 ', Job_status=1 (expired),,,,,, contact_name= Miss Wang, contact_number=0510-85550088,remarks= ' ★ This position is imported from the background
', enter_time= ' 2010-11-03 09:45:11.0 ', change_time=2010-12-03 02:18:05.0,,job_seq=123201,,required_min_age=22,,, accommodations= negotiable, serve_principal=wjw12580,job_summary= Junior College, more than 1 years working experience in 5s management, computer operation Proficiency,,}}
2010-12-03 13:23:02,302 [http-80-15] DEBUG mhr.roi.mhrmanager-mhrgetemployerreq={seq=0,employer_id= ' 86130469 '}
2010-12-03 13:23:02,304 [http-80-15] DEBUG mhr.roi.mhrmanager-mhrgetemployerrsp={seq=0,result=0 (success), info={ Employer_id= ' 86130469 ', employer_name= Wuxi Fuji Fashion Co., Ltd., employer_region=1902,employer_address= Wuxi Binhu, 8th, the Science and Technology Park,, employer _desc= Wuxi Fuji Fashion (Group) Co., Ltd. was founded in 1992, is a Sino-Japanese and Korean joint ventures. The main production of high-grade knitted fashion, the products are all exported, for many years won the "Top 500 chinese Industry", "the National industrial key industry benefits Ten Enterprises", "Wuxi Hundred good Enterprise" and so on. The company is headquartered in Wuxi, Jiangsu Province, Binhu City Shuicheng Science and Technology Park, a new plant facilities, covers an area of 30 acres. Companies in Suzhou, Taixing, Yancheng, Xuzhou has a branch, the group of existing staff more than 1500 people, annual sales of nearly 300 million yuan. ,,,,,, open_mode=5 (System automatic operation), open_time= ' 2010-11-03 09:45:10.0 ',,,,, contact_name= Miss Wang, contact_number=0510-85550088, ,,, Employer_status=1 (already open),,, login_password=871386,,agency=false,balance=100.0000,,,,, serve_principal=wjw12580 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, remarks= ' This account is "imported ' by 12580zgz-," Enter_time= ' 2010-11-03 09:45:10.0 '
CTRL + C
[Email protected] logs]#

Example three dynamic trace log4j logs

After my experiment, I found the Tail-f function is powerful, it is equivalent to--follow=name--retry. If the file you are tracking is moved or renamed, you also want to continue to tail it, you can use this option.

Tail manual page about--retry: Keep trying to open a file even if it's inaccessible when tail starts or if it becomes INACC Essible later; Useful when following by name, i.e., with--follow=name. Tail The file does not exist when the command is started or the file is inaccessible during execution, it is retried continuously.

Description of--follow:-F,--follow[={name|descriptor}] output appended data as the file grows;-F,--follow, and--follow=desc Riptor is equivalent--follow=descriptor indicates that the trace is a file descriptor, and--follow=name indicates that the file name is being traced. If the file name is changed and you want to keep track of the trailing content of the original file name, you have to use the-f option instead of the-f option.

[Email protected] imx_server]# tail-f log/imx. LOG
14:13:28.892 INFO imxconnection[6] Imx.server.imxconnection-rx imx_active_test{seq=3460,client_id=1291343201649042 , Presence_status=1 (Presence_status_online),}
14:13:28.892 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-006417 (01/02/00)-Connection #9 served
14:13:28.892 INFO imxconnection[6] imx.dbo.imxonlineinforow-exec SQL UPDATE imx_online_info SET last_active_time = ' 201 0-12-03 14:13:28.0 ' WHERE account = ' Zhy '
14:13:28.894 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-update imx_online_info SET last_active_time = '  2010-12-03 14:13:28.0 ' WHERE account = ' zhy '; (1 milliseconds)
14:13:28.894 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-006417 (00/02/00)-Connection #9 returned (now AVA ilable)
14:13:29.625 INFO imxconnection[6] Imx.server.imxconnection-rx imx_active_test{seq=3461,client_id=1291343201649042 , Presence_status=1 (Presence_status_online),}
14:13:29.626 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-006418 (01/02/00)-Connection #8 served
14:13:29.626 INFO imxconnection[6] imx.dbo.imxonlineinforow-exec SQL UPDATE imx_online_info SET last_active_time = ' 201 0-12-03 14:13:29.0 ' WHERE account = ' Zhy '
14:13:29.627 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-update imx_online_info SET last_active_time = '  2010-12-03 14:13:29.0 ' WHERE account = ' zhy '; (0 milliseconds)
14:13:29.653 DEBUG imxconnection[6] org.logicalcobwebs.proxool.imxdb-006418 (00/02/00)-Connection #8 returned (now AVA ilable)
CTRL + C
[Email protected] imx_server]#

To summarize: to track the renamed log, use Tail-f instead of tail-f.

Example four processing binary files

[email protected] ~]# seq >1.txt
[email protected] ~]# cat 1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[Email protected] ~]# hexdump-c 1.txt
00000000 0a 0a 0a, 0a, 0a, 0a, 0a, 0a |1.2.3.4.5.6.7.8.|
00000010 0a to 0a, 0a, 0a, 0a 31 34 |9.10.11.12.13.14|
00000020 0a 0a (0a)-0a---0a----0a 15.16.17.18.19.|
00000030 0a |20.|
00000033
[Email protected] ~]# Tail-c 1.txt | Hexdump-c
00000000 0a 0a, 0a, 0a |. 18.19.20.|
0000000a
[Email protected] ~]# tail-c -10 1.txt | Hexdump-c
00000000 0a 0a, 0a, 0a |. 18.19.20.|
0000000a
[Email protected] ~]# tail-c +10 1.txt | Hexdump-c
00000000 0a 0a, 0a, 0a, 0a, 0a, 0a 31 |. 6.7.8.9.10.11.1|
00000010 0a 0a, 0a, 0a, 0a 31 37 |2.13.14.15.16.17|
00000020 0a 0a, 0a, 0a |. 18.19.20.|
0000002a
[Email protected] ~]#

Tail monitoring log files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.