Home > System Tutorial > LINUX > body text

Master Linux command line monitoring output to better manage servers

WBOY
Release: 2024-02-12 20:40:12
forward
361 people have browsed it

For Linux system administrators, it is very important to know how to monitor output from the command line. It can help you view system status and performance data in real time, diagnose problems and take timely measures. This article will introduce several commonly used Linux command line tools to help you easily monitor the output of the server.

Sometimes it is necessary to continuously execute a command and track the changes in its output. A common method is to write an infinite loop shell script to achieve this, but Linux itself already provides a tool to help you regularly execute specified programs and output the results in full screen. This section will introduce the tool.

Concepts and terminology

When executing a command line on the shell, three standard files are usually automatically opened, namely the standard input file (stdin), which usually corresponds to the terminal keyboard; the standard output file (stdout) and the standard error output file (stderr). Both files correspond to the terminal screen. The process will get input data from the standard input file, output normal output data to the standard output file, and send error information to the standard error file.

Practical operation: Use watch to execute commands regularly and monitor the output

Basic usage of watch

The use of watch is very simple, just need:

watch 命令
Copy after login

That's it, so the watch command will execute the command every two seconds and output the execution results in full screen.

The picture below is an interface for executing watch date:

Every 2.0s: date                         orangepipc2: Sat Oct  3 09:55:14 2020
 
Sat 03 Oct 2020 09:55:14 AM CST
Copy after login

Every 2.0s in the first line of the figure: means watch executes a command every 2 seconds. The following date is the command to be executed. The following orangepipc2: is the host name where the command is executed, and Sat Oct 3 09:55:14 2020 is the time when the command is executed. Starting from the third line is the output result of command execution.
Master Linux command line monitoring output to better manage servers

Specify the interval between repeated execution of the command

You can also set the interval for repeated command execution through -n interval. For example, I can adjust it to execute the date command every 5 seconds.

watch -n 5 date
Copy after login

Discover changes in output

If you simply execute the command repeatedly, it can be easily achieved through a shell loop. However, the capabilities of watch are not limited to that.

With the -d option, watch can also highlight different parts of the two outputs. This function is quite practical:

watch -d -n 10 date
Copy after login

In addition to highlighting the changed parts of the output, you can also set watch to exit the loop execution when it finds that the results have changed, by using the -g/–chgexit option. For example, we can use the following command to discover USB changes.

watch -g 'dmesg |grep -i usb |tail'
Copy after login

Detect the return value of command execution
By default, watch does not care whether the execution result of the command is successful, but you can set watch to detect the return value of the command. When the command returns a non-zero value, it will beep (-b/-beep) or exit directly (- e/–errexit).

watch -e wrong_commands
Copy after login
Master Linux command line monitoring output to better manage servers

By learning the Linux command line tools mentioned in this article, you can monitor the server's output more efficiently and discover and solve potential problems in a timely manner. This will help maintain server stability and security, allowing your business to run more smoothly. Whether you are an experienced system administrator or a beginner, after mastering these skills, you will be more comfortable managing your Linux system.

The above is the detailed content of Master Linux command line monitoring output to better manage servers. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!