When asked to view logs during an interview, don’t just say tail, cat, more

First, how to view logs:

Many junior tester, to execute test cases in this step, find bugs, can not be more accurate to locate the bug, you can open a Linux server in this case, the command to view operations in real-time log, when the system error, can capture the log in the defect management system, the developer will know where is wrong, Make sure the operation steps are specified.

1, download Xshell, Baidu xshell download can be



2, create a session, use the account and password to log in to the server you use the system, to connect.

3. After the connection is successful, run the CD/XXX command to switch to the current working command. Continue to knock ls, list all files under the file, find the current system stored in that folder; Go to CD zhgl-server;



4. A log will be displayed

5, LS-LRT, show how many logs

6, CD logs, go to the logs folder, ls query the daily files, and then run the command,, tail -f localhost_access_log.2018-12-11.txt(current time) today’s real-time logs, operate the system, the corresponding log will be displayed

What other commands are often used to view logs?

\

Some programmers will like the following command:

Ps – ef | grep Java (consult the Java process ID)

Kill -9 PID(Use with caution in production environments)

Differences between the kill, killall, and pkill commands

Kill: Kill the process by pid

Killall (killall [parameter] [process name]) : In Linux, the killall command is used to kill processes by name. If you want to killa process with a specified PID, you can use the kill command to kill it. If you want to killa process, you need to use the ps command and grep command to find the process. Killall combines the two commands into one.

Pkill: Pkill is similar to killall in that it kills the program while it is running. If you want to kill a single process, use kill. Example: pkill -9 Firefox

1. View common log commands

   tail:

-n indicates the line number. Equivalent to nl command; Here’s an example:

Tail-100f test.log Monitors 100 line logs in real time

Tail -n 10 test.log Queries the last 10 rows in the tail of a log.

Tail -n +10 test.log Queries all logs after 10 rows;

    head:  

This is the opposite of tail, which is how many rows are in the log; Here’s an example:

Head -n 10 test.log Queries the first 10 lines in the log file.

Head -n-10 test.log Queries all logs except the last 10 lines in the log file;

Cat:

Tac is in reverse order, is the cat word written backwards; Here’s an example:

The cat – n test. The log | grep “debug” log query keywords

2. Application scenario 1: View by line number – Logs near the keyword are filtered out

1) the cat – n test. The log | grep “debug” get key log line number

2) cat error usually find the error log. The log | grep ‘Nick’, at this time, we have a requirement is the output current of the log before and after a few lines:

Cat. The error log | grep – B 5 ‘Nick’ before the show Nick and five rows

Cat error. The log | grep – A 5 ‘Nick’ show Nick and after 5 lines

Cat error. The log | grep -c 5 ‘Nick’ display file object matching Nick string up and down the line and line 5

Cat error. The log | grep A10 – n – B10-5 ‘Nick’ displays the file file in 10 lines before and after Nick string matching

3. Application scenario 2: Select a specific range of logs for analysis

1) the cat – n test. The log | tail – n + 1000 | head – n 20 starting line 1000, 20 lines

Tail -n +1000 indicates that the logs after 1000 rows are queried

Head -n 20 indicates that the first 20 records are checked in the previous query results

2) the cat catalina. Out | head – n 1400 | tail lines – n + 1350 show 1350 to 1400 (principle about the same, it is through grammar)

(1) Interception by date: Generally, the time of printing logs is recorded in the log system. Usually, we need to find the logs at the specified time end:

sed -n ‘/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p’ test.log

Note: The two date values in the command must be the values contained in the log file; otherwise, the command is invalid. Grep ‘2014-12-17 16:17:20’ test.log to check whether the time point exists in the log

(2) Cut according to the number of lines

Sed – n ‘10000200 p’ test. The log

Sed -i ‘/ keyword /d’ catalina.out deletes the line containing the keyword

4. Application Scenario 3: There are too many logs to view on the screen

(1) Using the more and less commands,

Such as: the cat – n test. The log | grep “debug” | more pages to print just the way they are, by clicking on the blank space key pages

(2) Use >xxx. TXT to save it in a file, then you can drop down this file for analysis

Such as: the cat – n test. The log | grep “debug” > debug. TXT

5: Use pipelines for and or condition processing

Implementation and use pipes For example, grep – n ‘log screen’ test. The log | grep ‘log’

Or use – E for example, grep – n – E ‘log screen | hello’ test. The log to satisfy two key words can find out