是否可以在linux命令行中每隔n秒重复一次命令。
例如,假设我正在运行导入,而我正在执行
ls -l
检查文件大小是否增加。我希望有一个命令让它自动重复。
答案 0 :(得分:510)
每5秒观看一次......
watch -n 5 ls -l
如果您希望直观地确认更改,请在--differences
命令之前附加ls
。
根据OSX手册页,还有
- 累积选项突出显示“粘性”,呈现一个 运行显示所有已更改的位置。 -t 或--no-title选项关闭显示间隔的标题, 命令,以及显示屏顶部的当前时间,以及 以下空行。
可以找到Linux / Unix手册页here
答案 1 :(得分:109)
while true; do
sleep 5
ls -l
done
答案 2 :(得分:86)
“观看”不允许在Busybox中使用一秒钟,而“睡眠”则可以。如果这对你很重要,试试这个:
while true; do ls -l; sleep .5; done
答案 3 :(得分:27)
INT
已经返回sleep
。因此,我正在使用:
0
这比mikhail的解决方案略短。一个小缺点是它在第一次运行目标命令之前就会休眠。
答案 4 :(得分:18)
如果命令包含一些特殊字符(如管道和引号),则需要使用引号填充命令。例如,要重复concc = 0
discc = 0
for (i in 1:136){
##Chosing the Min1 and Min2 names (it changes between data.frames but here it reveals NULL because understand paste as character)
assign('parsel' ,colnames<-(paste0("compl00", i)))
for (j in 1:length(paste0("compl00", i))){
if ((assign("a",(paste0("compl00",i,'$',parsel[1],'[',j,']'))))==(assign('b',paste0("compl00",i,'$',parsel[2],'[',j+1,']'))))
{concc = concc + 1}
else
{discc = discc + 1}
}
}
,watch命令应为:
ls -l | grep "txt"
答案 5 :(得分:13)
当我们使用while
时,可以在没有cron的情况下定期运行命令。
作为命令:
while true ; do command ; sleep 100 ; done &
[ ex: # while true; do echo `date` ; sleep 2 ; done & ]
示例:
while true
do echo "Hello World"
sleep 100
done &
不要忘记最后一个&
,因为它会将你的循环放在后台。但是你需要使用命令“ps -ef | grep your_script”找到进程id,然后你需要杀死它。所以请加上'&amp;'当你运行脚本时。
# ./while_check.sh &
这是与脚本相同的循环。创建文件“while_check.sh”并将其放入其中:
#!/bin/bash
while true; do
echo "Hello World" # Substitute this line for whatever command you want.
sleep 100
done
然后输入bash ./while_check.sh &
答案 6 :(得分:7)
手表很好,但会清洁屏幕。
watch -n 1 'ps aux | grep php'
答案 7 :(得分:6)
如果你想避免“漂移”,意味着你希望命令每N秒执行一次,无论命令花了多长时间(假设它花费的时间少于N秒),这里有一些bash会每隔5秒重复一次命令精度为1秒(如果无法跟上,将打印出警告):
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
由于睡眠仅精确到一秒钟,它可能仍会漂移一点。您可以通过创造性地使用日期命令来提高此准确性。
答案 8 :(得分:4)
您可以运行以下内容并仅过滤大小。如果您的文件名为somefilename
,则可以执行以下操作
while :; do ls -lh | awk '/some*/{print $5}'; sleep 5; done
众多想法中的一个。
答案 9 :(得分:4)
如果您想要执行特定次数的操作,您可以随时执行此操作:
repeat 300 do my first command here && sleep 1.5
答案 10 :(得分:1)
为了更轻松地减少漂移,请使用:
while :; do sleep 1m & some-command; wait; done
由于bash的时间运行循环结构和睡眠命令实际执行,仍然会有一点漂移。
提示:&#39;:&#39; evals to 0 ie true。
答案 11 :(得分:0)
watch -n 5 'ls -l
每5秒后运行ls -l
命令
Every 5.0s: ls -l Fri Nov 17 16:28:25 2017
total 169548
-rw-rw-r-- 1 sachin sachin 4292 Oct 18 12:16 About_us_Admission.doc
-rw-rw-r-- 1 sachin sachin 865 Oct 13 15:26 About_us_At_glance.doc
-rw-rw-r-- 1 sachin sachin 1816 Oct 13 16:11 About_us_Principle.doc
-rw-rw-r-- 1 sachin sachin 1775 Oct 13 15:59 About_us_Vission_mission.doc
-rw-rw-r-- 1 sachin sachin 1970 Oct 13 16:41 Academic_Middle_school.doc
-rw-rw-r-- 1 sachin sachin 772 Oct 16 16:07 academics_High_School.doc
-rw-rw-r-- 1 sachin sachin 648 Oct 16 13:34 academics_pre_primary.doc
-rw-rw-r-- 1 sachin sachin 708 Oct 16 13:39 academics_primary.doc
-rwxrwxr-x 1 sachin sachin 8816 Nov 1 12:10 a.out
-rw-rw-r-- 1 sachin sachin 23956 Oct 23 18:14 Ass1.c++
-rw-rw-r-- 1 sachin sachin 342 Oct 23 22:13 Ass2.doc
drwxrwxr-x 2 sachin sachin 4096 Oct 19 10:45 Backtracking
drwxrwxr-x 3 sachin sachin 4096 Sep 23 20:09 BeautifulSoup
drwxrwxr-x 2 sachin sachin 4096 Nov 2 00:18 CL_1
drwxrwxr-x 2 sachin sachin 4096 Oct 23 20:16 Code
drwxr-xr-x 2 sachin sachin 4096 Nov 15 12:05 Desktop
-rw-rw-r-- 1 sachin sachin 0 Oct 13 23:12 doc
drwxr-xr-x 4 sachin sachin 4096 Nov 6 21:18 Documents
drwxr-xr-x 27 sachin sachin 12288 Nov 17 13:23 Downloads
-rw-r--r-- 1 sachin sachin 8980 Sep 19 23:58 examples.desktop
答案 12 :(得分:0)
简洁的解决方案,如果您想重复运行命令直到失败,则尤其有用,
while ls -l; do
sleep 5
done