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 to use watch to help you repeat orders

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

Share

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

Editor to share with you how to use watch to help you repeat orders, I believe most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Watch command

Watch is a very useful command, almost all Linux distributions come with this gadget, like the name, watch can help you monitor the results of a command, saving you from running it manually over and over again. Under Linux, watch periodically executes the next program and displays the results of the execution on full screen. You can use it to monitor the changes in the results of all the commands you want, such as tail a log file, ls to monitor the size change of a file, depending on your imagination!

1. Command format:

Watch [parameters] [command]

2. Command function:

The output result of the command can be output to the standard output device, which is mostly used to execute the command periodically / regularly.

3. Command parameters:

-n or-- interval watch runs the program every 2 seconds by default, and you can specify the interval with-n or-interval.

-d or-- differences use the-d or-- differences option watch to highlight the changed area. The-d=cumulative option highlights where it has changed (regardless of the most recent change or not).

-t or-no-title closes the output of the watch command at the top of the interval, the command, the current time.

-h,-- help view help documentation

Introduction

Sometimes you need to keep executing a command to track changes in its output. You might write an endless loop to do this:

While: do clear commands sleep 1done

In fact, however, there is a watch command in linux that can do this for you. It periodically executes the specified program and outputs the results full screen.

The use of watch is simple, you only need to

Watch command

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

As you can see from the figure above, Every 2.0s in the first line indicates that watch executes commands every 2 seconds. The following date is the command to be executed. The next T520: Thu May 10 16:55:23 2018 is the hostname and the time the command was executed.

Below, it is time for the command to be executed from the second line.

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

Watch-n 5 date

Not only that, watch can also highlight different parts of the two outputs with the-d option, which is quite practical

Watch-d-n 1 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 by using the-g/--chgexit option

Watch-g free

By default, watch does not care about the success of the execution of the command

But you can ask 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

Finally, if you want watch to show only the execution results of the command, rather than the information on the first line, you can use the-t option to turn off the display of title

Watch-t date

The above is all the contents of the article "how to use watch to help you repeat orders". 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

Servers

Wechat

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

12
Report