在 Linux 中自动重复命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13593771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Repeat command automatically in Linux
提问by Marty Wallace
Is it possible in Linux command line to have a command repeat every nseconds?
在 Linux 命令行中是否可以每n秒重复一次命令?
Say, I have an import running, and I am doing
说,我有一个导入正在运行,我正在做
ls -l
to check if the file size is increasing. I would like to have a command to have this repeat automatically.
检查文件大小是否增加。我想要一个命令让这个自动重复。
采纳答案by Rawkode
Watch every 5 seconds ...
每 5 秒看一次...
watch -n 5 ls -l
watch -n 5 ls -l
If you wish to have visual confirmation of changes, append --differences
prior to the ls
command.
如果您希望对更改进行视觉确认,请--differences
在ls
命令之前附加。
According to the OSX man page, there's also
根据 OSX 手册页,还有
The --cumulative option makes highlighting "sticky", presenting a running display of all positions that have ever changed. The -t or --no-title option turns off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.
--cumulative 选项突出显示“粘性”,显示所有已更改位置的运行显示。-t 或 --no-title 选项关闭在显示屏顶部显示间隔、命令和当前时间的标题,以及后面的空行。
Linux/Unix man page can be found here
可以在此处找到 Linux/Unix 手册页
回答by Oleksandr Kravchuk
while true; do
sleep 5
ls -l
done
回答by Valentin Bajrami
You can run the following and filter the size only. If your file was called somefilename
you can do the following
您可以运行以下命令并仅过滤大小。如果您的文件被调用, somefilename
您可以执行以下操作
while :; do ls -lh | awk '/some*/{print $5}'; sleep 5; done
while :; do ls -lh | awk '/some*/{print $5}'; sleep 5; done
One of the many ideas.
许多想法之一。
回答by mikhail
"watch" does not allow fractions of a second in Busybox, while "sleep" does. If that matters to you, try this:
“watch”不允许在 Busybox 中使用几分之一秒,而“sleep”允许。如果这对您很重要,请尝试以下操作:
while true; do ls -l; sleep .5; done
回答by brightball
If you want to do something a specific number of times you can always do this:
如果您想做某事特定次数,您可以随时执行以下操作:
repeat 300 do my first command here && sleep 1.5
回答by Ranjithkumar T
Running commands periodically without cron is possible when we go with while
.
当我们使用while
.
As a command:
作为命令:
while true ; do command ; sleep 100 ; done &
[ ex: # while true; do echo `date` ; sleep 2 ; done & ]
Example:
例子:
while true
do echo "Hello World"
sleep 100
done &
Do not forget the last &
as it will put your loop in the background. But you need to find the process id with command "ps -ef | grep your_script" then you need to kill it. So kindly add the '&' when you running the script.
不要忘记最后一个,&
因为它会将您的循环置于后台。但是您需要使用命令“ps -ef | grep your_script”找到进程ID,然后您需要杀死它。因此,请在运行脚本时添加“&”。
# ./while_check.sh &
Here is the same loop as a script. Create file "while_check.sh" and put this in it:
这是与脚本相同的循环。创建文件“while_check.sh”并将其放入其中:
#!/bin/bash
while true; do
echo "Hello World" # Substitute this line for whatever command you want.
sleep 100
done
Then run it by typing bash ./while_check.sh &
然后通过键入运行它 bash ./while_check.sh &
回答by mcote
If you want to avoid "drifting", meaning you want the command to execute every N seconds regardless of how long the command takes (assuming it takes less than N seconds), here's some bash that will repeat a command every 5 seconds with one-second accuracy (and will print out a warning if it can't keep up):
如果你想避免“漂移”,这意味着你希望命令每 N 秒执行一次,而不管命令需要多长时间(假设它需要少于 N 秒),这里有一些 bash 会每 5 秒重复一次命令,其中一个 -第二个精度(如果跟不上会打印出警告):
PERIOD=5
while [ 1 ]
do
let lastup=`date +%s`
# do command
let diff=`date +%s`-$lastup
if [ "$diff" -lt "$PERIOD" ]
then
sleep $(($PERIOD-$diff))
elif [ "$diff" -gt "$PERIOD" ]
then
echo "Command took longer than iteration period of $PERIOD seconds!"
fi
done
It may still drift a little since the sleep is only accurate to one second. You could improve this accuracy by creative use of the date command.
由于睡眠仅精确到一秒,因此它可能仍然会漂移一点。您可以通过创造性地使用 date 命令来提高这种准确性。
回答by jonathanzh
If the command contains some special characters such as pipes and quotes, the command needs to be padded with quotes. For example, to repeat ls -l | grep "txt"
, the watch command should be:
如果命令中包含一些特殊字符,如管道和引号,则需要用引号填充命令。例如,要重复ls -l | grep "txt"
,监视命令应该是:
watch -n 5 'ls -l | grep "txt"'
watch -n 5 'ls -l | grep "txt"'
回答by Eduardo
watch is good but will clean the screen.
手表很好,但会清洁屏幕。
watch -n 1 'ps aux | grep php'
回答by Sebastian Wagner
sleep
already returns 0
. As such, I'm using:
sleep
已经返回0
。因此,我正在使用:
while sleep 3 ; do ls -l ; done
This is a tiny bit shorter than mikhail's solution. A minor drawback is that it sleeps before running the target command for the first time.
这比 mikhail 的解决方案要短一点。一个小缺点是它在第一次运行目标命令之前休眠。