Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How linux monitors command output

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how linux monitors command output, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Concepts and terminology

When you execute a command line on shell, you usually open three standard files automatically, namely, the standard input file (stdin), which usually corresponds to the terminal's keyboard; the standard output file (stdout) and the standard error output file (stderr), both of which correspond to the terminal screen. The process will get the input data from the standard input file, output the normal output data to the standard output file, and send the error information to the standard error file.

Operational scenario 1: use watch to execute commands periodically and monitor output difficulty ★ demo with distribution Ubuntu involving the basic use of the command watchwatch

The way to use watch is simple, just:

Watch command

That's it, so that the watch command executes the command every two seconds and outputs the execution result full screen.

The following figure shows an interface for performing watch date:

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

Every 2.0s in the first line of the figure: indicates that watch executes commands every 2 seconds. The following date is the command to be executed. The following orangepipc2: is the hostname of the command, and Sat Oct 3 09:55:14 2020 is the time to execute the command.

Starting from the third line is the output of the command execution.

Specify the interval between repeated execution of the command

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

Watch-n 5 date finds changes in the output

If you simply repeat the command, you can easily do it through the shell loop, but the power of watch is more than that.

With the-d option, watch can also highlight different parts of the two outputs, which is quite useful:

Watch-d-n 10 date

In addition to highlighting the changes in the output, you can also set the watch to exit the loop when it finds a change in the result, using the-g/--chgexit option. For example, we can find USB changes with the following command.

Watch-g 'dmesg | grep-I usb | return value of tail' detection command execution

By default, watch doesn't care about the success of the command, but you can set watch to detect the return value of the command, beep (- b/--beep) or exit directly (- e/--errexit) when the command returns non-zero.

Watch-e wrong_commands

The above is all the contents of the article "how linux monitors command output". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report