mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
commit
07b5f110b6
@ -1,148 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (Starryi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Watching activity on Linux with watch and tail commands)
|
||||
[#]: via: (https://www.networkworld.com/article/3529891/watching-activity-on-linux-with-watch-and-tail-commands.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
Watching activity on Linux with watch and tail commands
|
||||
======
|
||||
The watch and tail commands can help monitor activity on Linux systems. This post looks at some helpful ways to use these commands.
|
||||
Loops7 / Getty Images
|
||||
|
||||
The **watch** and **tail** commands provide some interesting options for examining activity on a Linux system in an ongoing manner.
|
||||
|
||||
That is, instead of just asking a question and getting an answer (like asking **who** and getting a list of currently logged in users), you can get **watch** to provide you with a display showing who is logged in along with updates as users come and go.
|
||||
|
||||
[[Get regularly scheduled insights by signing up for Network World newsletters.]][1]
|
||||
|
||||
With **tail**, you can display the bottoms of files and see content as it is added. This kind of monitoring is often very helpful and requires less effort than running commands periodically.
|
||||
|
||||
### Using watch
|
||||
|
||||
One of the simplest examples of using **watch** is to use the command **watch who**. You should see a list showing who is logged in along with when they logged in and where they logged in from. Notice that the default is to update the display every two seconds (top left) and that the date and time (upper right) updates itself at that interval. The list of users will grow and shrink as users log in and out.
|
||||
|
||||
### $ watch who
|
||||
|
||||
This command will dissplay a list of logins like this:
|
||||
|
||||
```
|
||||
Every 2.0s: who dragonfly: Thu Feb 27 10:52:00 2020
|
||||
|
||||
nemo pts/0 2020-02-27 08:07 (192.168.0.11)
|
||||
shs pts/1 2020-02-27 10:58 (192.168.0.5)
|
||||
```
|
||||
|
||||
You can change the interval to get less frequent updates by adding a **-n** option (e.g., -n 10) to select a different number of seconds between updates.
|
||||
|
||||
### $ watch -n 10 who
|
||||
|
||||
The new interval will be displayed and the time shown will change less frequently, aligning itself with the selected interval.
|
||||
|
||||
[][2]
|
||||
|
||||
```
|
||||
Every 10.0s: who dragonfly: Thu Feb 27 11:05:47 2020
|
||||
|
||||
nemo pts/0 2020-02-27 08:07 (192.168.0.11)
|
||||
shs pts/1 2020-02-27 10:58 (192.168.0.5)
|
||||
```
|
||||
|
||||
If you prefer to see only the command's output and not the heading (the top 2 lines), you can omit those lines by adding the **-t** (no title) option.
|
||||
|
||||
### $ watch -t who
|
||||
|
||||
Your display will then look like this:
|
||||
|
||||
```
|
||||
nemo pts/0 2020-02-27 08:07 (192.168.0.11)
|
||||
shs pts/1 2020-02-27 10:58 (192.168.0.5)
|
||||
```
|
||||
|
||||
If every time the watched command runs, its output is the same, only the title line (if not omitted) will change. The rest of the displayed information will stay the same.
|
||||
|
||||
If you want your **watch** command to exit as soon as the output of the command that it is watching changes, you can use a **-g** (think of this as the "go away") option. You might choose to do this if, for example, you are simply waiting for others to start logging into the system.
|
||||
|
||||
You can also highlight changes in the displayed output using the **-d** (differences) option. The highlighting will only last for one interval (2 seconds by default), but can help to draw your attention to the changes.
|
||||
|
||||
Here's a more complex example of using the **watch** command to display services that are listening for connections and the ports they are using. While the output isn't likely to change, it would alert you to any new service starting up or one going down.
|
||||
|
||||
```
|
||||
$ watch 'sudo lsof -i -P -n | grep LISTEN'
|
||||
```
|
||||
|
||||
Notice that the command being run needs to be enclosed in quotes to ensure that the **watch** command doesn't send its output to the grep command.
|
||||
|
||||
Using the **watch -h** command will provide you with a list of the command's options.
|
||||
|
||||
```
|
||||
$ watch -h
|
||||
|
||||
Usage:
|
||||
watch [options] command
|
||||
|
||||
Options:
|
||||
-b, --beep beep if command has a non-zero exit
|
||||
-c, --color interpret ANSI color and style sequences
|
||||
-d, --differences[=<permanent>]
|
||||
highlight changes between updates
|
||||
-e, --errexit exit if command has a non-zero exit
|
||||
-g, --chgexit exit when output from command changes
|
||||
-n, --interval <secs> seconds to wait between updates
|
||||
-p, --precise attempt run command in precise intervals
|
||||
-t, --no-title turn off header
|
||||
-x, --exec pass command to exec instead of "sh -c"
|
||||
|
||||
-h, --help display this help and exit
|
||||
-v, --version output version information and exit
|
||||
```
|
||||
|
||||
### Using tail -f
|
||||
|
||||
The **tail -f** command has something in common with **watch**. It will both display the bottom of a file and additional content as it is added. Instead of having to run a "tail" command again and again, you run one command and get a repeatedly updated view of its output. For example, you could watch a system log with a command like this:
|
||||
|
||||
```
|
||||
$ tail -f /var/log/syslog
|
||||
```
|
||||
|
||||
Some files, like **/var/log/wtmp**, don't lend themselves to this type of handling because they're not formatted as normal text files, but you could get a similar result by combining **watch** and **tail** like this:
|
||||
|
||||
```
|
||||
watch 'who /var/log/wtmp | tail -20'
|
||||
```
|
||||
|
||||
This command will display the most recent 5 logins regardless of how many of the users are still logged in. If another login occurs, a line will be added and the top line removed.
|
||||
|
||||
```
|
||||
Every 60.0s: who /var/log/wtmp | tail -5 dragonfly: Thu Feb 27 12:46:07 2020
|
||||
|
||||
shs pts/0 2020-02-27 08:07 (192.168.0.5)
|
||||
nemo pts/1 2020-02-27 08:26 (192.168.0.5)
|
||||
shs pts/1 2020-02-27 10:58 (192.168.0.5)
|
||||
nemo pts/1 2020-02-27 11:34 (192.168.0.5)
|
||||
dory pts/1 2020-02-27 12:14 (192.168.0.5)
|
||||
```
|
||||
|
||||
Both the **watch** and **tail -f** commands can provide auto-updating views of information that you might at times want to monitor, making the task of monitoring quite a bit easier whether you're monitoring processes, logins or system resources.
|
||||
|
||||
Join the Network World communities on [Facebook][3] and [LinkedIn][4] to comment on topics that are top of mind.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3529891/watching-activity-on-linux-with-watch-and-tail-commands.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.networkworld.com/newsletters/signup.html
|
||||
[2]: https://www.networkworld.com/article/3440100/take-the-intelligent-route-with-consumption-based-storage.html?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE21620&utm_content=sidebar ( Take the Intelligent Route with Consumption-Based Storage)
|
||||
[3]: https://www.facebook.com/NetworkWorld/
|
||||
[4]: https://www.linkedin.com/company/network-world
|
@ -0,0 +1,145 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (Starryi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Watching activity on Linux with watch and tail commands)
|
||||
[#]: via: (https://www.networkworld.com/article/3529891/watching-activity-on-linux-with-watch-and-tail-commands.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
使用 watch 和 tail 命令监视 Linux 上的活动
|
||||
======
|
||||
watch 和 tail 命令可以帮助监视 Linux 系统上的活动。本文介绍了这两个命令的一些有用的使用方法。
|
||||
|
||||
**watch** 和 **tail** 命令为持续监视 Linux 系统上的活动提供了一些有趣的选项。
|
||||
|
||||
也就是说,你可以通过 **watch** 来显示谁已登录,并随着用户登陆和注销,更新显示内容,而不是仅仅提出问题并获得答案(例如询问 **who** 并获取当前登录用户的列表)。
|
||||
|
||||
使用 **tail**,您可以显示文件的底部并在添加内容时查看内容。这种监控一般非常有用,并且比定期运行命令所需的工作更少。
|
||||
|
||||
### 使用 watch 命令
|
||||
|
||||
使用 **watch** 的最简单示例之一是使用命令 **watch who**。你会看到一个列表,其中显示了谁登录了,以及他们登录的时间和登录位置。请注意,默认设置是每两秒更新一次显示(左上角),日期和时间(右上角)将按该间隔自行更新。用户列表将随着用户登录和注销而增长和缩小。
|
||||
|
||||
### $ watch who
|
||||
|
||||
此命令将显示如下所示的登录列表:
|
||||
|
||||
```
|
||||
Every 2.0s: who dragonfly: Thu Feb 27 10:52:00 2020
|
||||
|
||||
nemo pts/0 2020-02-27 08:07 (192.168.0.11)
|
||||
shs pts/1 2020-02-27 10:58 (192.168.0.5)
|
||||
```
|
||||
|
||||
你可以通过添加 **-n** 选项(例如 -n 10)来修改更新间的不同秒数,以修改更新间隔,从而获取较少的更新频率。
|
||||
|
||||
### $ watch -n 10 who
|
||||
|
||||
上述命令将以新的间隔显示,并且显示的时间更新频率较低,从而使显示时间与所选间隔保持一致。
|
||||
|
||||
[][2]
|
||||
|
||||
```
|
||||
Every 10.0s: who dragonfly: Thu Feb 27 11:05:47 2020
|
||||
|
||||
nemo pts/0 2020-02-27 08:07 (192.168.0.11)
|
||||
shs pts/1 2020-02-27 10:58 (192.168.0.5)
|
||||
```
|
||||
|
||||
如果你希望仅查看命令的输出,而不是标题(前 2 行),则可以通过添加 **-t**(无标题)选项来省略这些行。
|
||||
|
||||
### $ watch -t who
|
||||
|
||||
然后,你的屏幕将显示如下所示:
|
||||
|
||||
```
|
||||
nemo pts/0 2020-02-27 08:07 (192.168.0.11)
|
||||
shs pts/1 2020-02-27 10:58 (192.168.0.5)
|
||||
```
|
||||
|
||||
如果每次运行监视的命令时,输出都是相同的,则只有标题行(如果未省略)会更改。其余显示的信息将保持不变。
|
||||
|
||||
如果你希望 **watch** 命令在它正在监视的命令的输出发生更新后立即退出,则可以使用 **-g**(将其视为“离开”)选项。例如,如果你只是在等待其他人开始登录系统,则可以选择执行此操作。
|
||||
|
||||
你还可以使用 **-d**(差异)选项突出显示显示输出中的更改。突出显示只会持续一个间隔(默认为 2 秒),但有助于引起你对更新的注意。
|
||||
|
||||
下面是一个更复杂的示例,该示例使用 **watch** 命令显示正在侦听连接的服务及其使用的端口。虽然输出不太可能更改,但它会提醒你任何新服务正在启动或关闭。
|
||||
|
||||
```console
|
||||
$ watch 'sudo lsof -i -P -n | grep LISTEN'
|
||||
```
|
||||
|
||||
值得注意的是,正在运行的命令需要用引号扩起来,以确保 **watch** 命令不会将其输出发送到 grep 命令。
|
||||
|
||||
使用 **watch -h** 命令将为你提供命令选项的列表。
|
||||
|
||||
```console
|
||||
$ watch -h
|
||||
|
||||
Usage:
|
||||
watch [options] command
|
||||
|
||||
Options:
|
||||
-b, --beep beep if command has a non-zero exit
|
||||
-c, --color interpret ANSI color and style sequences
|
||||
-d, --differences[=<permanent>]
|
||||
highlight changes between updates
|
||||
-e, --errexit exit if command has a non-zero exit
|
||||
-g, --chgexit exit when output from command changes
|
||||
-n, --interval <secs> seconds to wait between updates
|
||||
-p, --precise attempt run command in precise intervals
|
||||
-t, --no-title turn off header
|
||||
-x, --exec pass command to exec instead of "sh -c"
|
||||
|
||||
-h, --help display this help and exit
|
||||
-v, --version output version information and exit
|
||||
```
|
||||
|
||||
### 使用 tail -f
|
||||
|
||||
**tail -f** 命令与 **watch** 有一些相同之处。它也会在添加文件时显示文件的底部和其他内容。你不必一次又一次地运行 “tail” 命令,而是运行一个命令并获得可重复更新显示视图的结果。例如,你可以使用如下命令查看系统日志:
|
||||
|
||||
```console
|
||||
$ tail -f /var/log/syslog
|
||||
```
|
||||
|
||||
某些文件(如 **/var/log/wtmp**)不适合这种类型的处理,因为它们的格式不是普通文本文件,但是通过组合 **watch** 和 **tail**,你可以获得类似的结果,如下所示:
|
||||
|
||||
```bash
|
||||
watch 'who /var/log/wtmp | tail -20'
|
||||
```
|
||||
|
||||
无论有多少用户仍处于登录状态, 此命令都将只显示最近的 5 次登录。如果发生其他登录,显示结果将添加一行记录并删除顶行记录。
|
||||
|
||||
```
|
||||
Every 60.0s: who /var/log/wtmp | tail -5 dragonfly: Thu Feb 27 12:46:07 2020
|
||||
|
||||
shs pts/0 2020-02-27 08:07 (192.168.0.5)
|
||||
nemo pts/1 2020-02-27 08:26 (192.168.0.5)
|
||||
shs pts/1 2020-02-27 10:58 (192.168.0.5)
|
||||
nemo pts/1 2020-02-27 11:34 (192.168.0.5)
|
||||
dory pts/1 2020-02-27 12:14 (192.168.0.5)
|
||||
```
|
||||
|
||||
对你有时可能想要监视的信息,无论监视进程、登录名还是系统资源,**watch** 和 **tail -f** 命令都可以提供自动更新视图,从而使监视任务变得更加容易。
|
||||
|
||||
加入 [Facebook][3] 和 [LinkedIn][4] 上的网络世界社区,对最重要的话题发表评论。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3529891/watching-activity-on-linux-with-watch-and-tail-commands.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[Starryi](https://github.com/Starryi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.networkworld.com/newsletters/signup.html
|
||||
[2]: https://www.networkworld.com/article/3440100/take-the-intelligent-route-with-consumption-based-storage.html?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE21620&utm_content=sidebar ( Take the Intelligent Route with Consumption-Based Storage)
|
||||
[3]: https://www.facebook.com/NetworkWorld/
|
||||
[4]: https://www.linkedin.com/company/network-world
|
Loading…
Reference in New Issue
Block a user