ubuntu定时执行命令_命令行基础知识:定时命令执行

ubuntu定时执行命令

The command-line is full of a bunch of small, single purpose utilities that can help eliminate the need of writing any additional code yourself. Sure, you could track the start and end times and calculate the difference to get the duration. You could even grab your trusty stopwatch [app] and track things that way. Or, you could save yourself all that trouble and simply leverage time.

命令行中充满了一堆小型的,单一用途的实用程序,这些实用程序可以帮助您消除自己编写任何其他代码的需要。 当然,您可以跟踪开始时间和结束时间并计算差值以获得持续时间。 您甚至可以拿起值得信赖的秒表[app]并以这种方式进行跟踪。 或者,您可以省去所有麻烦,而只需利用time

入门 (Getting started)

For a such a simple command, time is actually a bit confusing, conceptionally.

对于这样一个简单的命令,从概念上讲, time实际上有点令人困惑。

First off, the time command is in no way related to the date command which provides the system’s date and time.

首先, time命令与提供系统日期和时间的date命令无关。

Next, time exists both as a built-in for some shells, like bash and zsh as well as a stand-alone command, known as GNU time.

接下来, time既作为某些shell(如bashzsh的内置组件,又作为独立命令存在,即GNU time

If you were to type in man time, you will most likely be presented with the man page for the stand-alone command, which features a bunch of different arguments than the time built into shells actually supports.

如果要输入man time ,很可能会看到独立命令的man页,该命令的功能与shell实际支持的time不同。

To make matters worse, the fish shell doesn’t seem to provide it’s own implementation of time. If happen to be using fish, you will want to make sure you have the time command installed.

更糟糕的是,该fish贝似乎并没有提供它自己的执行time 。 如果碰巧在使用fish ,则需要确保已安装time命令。

And to take it all a step further, if you’re using bash or zsh and have the GNU version of time installed, you have to reference it with a backslash, \time, else your shell will use it’s built-in version instead.

更进一步,如果您使用的是bashzsh并安装了GNU版本的time ,则必须使用反斜杠\time引用它,否则您的shell将使用其内置版本。

At their core though, they both can serve the same purpose, with the GNU version allowing more advanced formatting options.

不过,它们的核心都可以达到相同的目的,而GNU版本允许使用更高级的格式设置选项。

With all that said, both “versions” of time are non-destructive in nature, and the example commands we’ll be timing below will also be non-destructive as well. If so desired, time can very well be used with destructive commands like rm.

综上所述, time两个“版本”本质上都是无损的,下面我们将定时的示例命令也将是无损的。 如果需要的话, time可以很好地与rm类的破坏性命令一起使用。

The examples below (unless otherwise noted) will be using time that’s built into my favorite shell, and the new default shell on macOS, zsh.

下面的示例(除非另有说明)将使用我最喜欢的shell中内置的time以及macOS zsh上的新默认shell。

定时命令执行 (Timing command execution)

Timing the execution duration of a command is as easy as prefixing your command (any command) with time. A great command for trying this out is the tree command on the root / of your file system, which will list out a visual tree of all of your files and directories:

定时执行命令的时间就像在命令(任何命令)前加上time 。 一个很好的尝试命令是文件系统根目录/上的tree命令,它将列出所有文件和目录的可视树:

$ time tree /

# Long dramatic pause while a bunch of text scrolls on by...

166590 directories, 1568127 files
tree /  12.24s user 10.37s system 66% cpu 33.827 total

Notice the command we executed, tree / is the echoed back by the time command. We’ll skip over the next stuff for right now, and focus just on total and the number just prior. That’s how long the command took to execute, in seconds.

注意我们执行的命令, tree /time命令的回显。 现在,我们将跳过下一个内容,仅关注total和之前的数量。 这是命令执行所需的时间(以秒为单位)。

The time command, also works if you happen to bail on a command with CTRL-C. If we were to run time tree / again and quickly hit CTRL-C the tree will stop scrolling by and we’ll be presented with the time results for the time the command was executing before we killed it.

如果您碰巧使用CTRL-C保释了命令,那么time命令也将起作用。 如果要再次运行time tree /并快速按CTRL-Ctree将停止滚动,并且在终止之前,将显示命令执行时的time结果。

Try it out!

试试看!

跟踪资源使用情况 (Tracking resource usage)

The output from time includes 3 values, in addition to the total duration. The first is the total amount of time (in CPU-seconds) that the command spent in user mode. That’s the value with user after it.

除了总持续时间外, time输出还包括3个值。 第一个是命令在用户模式下花费的总时间(以CPU秒为单位)。 这就是user追求的价值。

The next, suffixed by system is the amount of time (again, in CPU-seconds) that the command spent in system, or kernel mode. Finally, the percentage of the CPU that was allocated to the command, suffixed with cpu.

接下来的后缀是system在系统或内核模式下花费的时间(再次以CPU秒为单位)。 最后,分配给命令的CPU百分比,后缀为cpu

用户和内核模式到底是什么? (What the heck are user and kernel modes anyway?)

Glad you asked! CPU usage is broken down by access levels.

很高兴你问! CPU使用率按访问级别细分。

When code is executed in user mode, it doesn’t have direct access to hardware or reference memory and must rely on APIs of the system for delegation. This is how most code runs on your system, and due to it’s isolation, crashes are always recoverable.

在用户模式下执行代码时,它无法直接访问硬件或参考内存,并且必须依赖系统的API进行委派。 这就是大多数代码在系统上运行的方式,并且由于其隔离性,崩溃总是可以恢复的。

Kernel mode on the other hand, is when code being executed has unrestricted access to the system hardware. Think of it like the most super user access you can have on a system.

另一方面,内核模式是指正在执行的代码可以不受限制地访问系统硬件。 可以将其su您在系统上拥有的最多用户访问权限。

This mode is pretty much reserved for the most trusted functions of the operating system. Because of having complete access, when things crash in kernel mode, they crash bad and tend to take the system down with them.

此模式几乎保留给操作系统的最受信任功能。 由于具有完全访问权限,因此当事物在内核模式下崩溃时,它们崩溃得很严重,并且往往使系统崩溃。

结论 (Conclusion)

I had originally planned to call this post time and time again, but as you can see, the time that’s built into your command-line shell is fairly limited and doesn’t really “time again”.

我原本打算把这个职位timetime再次 ,但正如你所看到的, time是内置到您的命令行shell是相当有限的,真正“不确实time再次”。

Even though it’s extremely limited in comparison to it’s stand-alone GNU brethren, your shell’s time still allows you quick and easy access to timing the execution duration of a command without any additional scripting!

尽管与独立的GNU兄弟相比,它的使用受到极大限制,但是您的shell time仍然可以让您快速轻松地访问定时命令执行时间,而无需任何其他脚本!

翻译自: https://www.digitalocean.com/community/tutorials/workflow-timing-command-execution

ubuntu定时执行命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值