如何使 "find" 命令变慢

How to make "find" command slow

您没看错:我想让 Linux 的 "find" 命令变慢并使用更少的系统资源。我创建了一个 cronjob,每 10 分钟 运行s 一次查找命令,但是当它 运行s 时,find 命令几乎使用了我的所有 CPU 大约 3 分钟,几乎没有留下任何东西给我所以。

所以我希望能够使 "find" 命令 运行 慢一点,这样它就不会占用我的 Centos 服务器的太多资源,并且 运行 在10 分钟而不是使用所有资源 3 分钟。

可以吗?

根据this

The nice command tweaks the priority level of a process so that it runs less frequently. This is useful when you need to run a CPU intensive task as a background or batch job. The niceness level ranges from -20 (most favorable scheduling) to 19 (least favorable). Processes on Linux are started with a niceness of 0 by default. The nice command (without any additional parameters) will start a process with a niceness of 10. At that level the scheduler will see it as a lower priority task and give it less CPU resources.

所以你可以简单地将你的任务包装在一个 nice 中,这将改变任务的优先级。

改变

whatever

nice whatever

你不能让它在内部变慢,除非你可以重新编译代码并添加一些延迟功能..

这里是如何减慢或延迟查找命令迭代执行的方法

find -type f -exec sh -c 'echo {};sleep 1'  \;

在打印文件名或(您喜欢执行的任何代码)之前每 1 秒让它休眠一次。