使用脚本以一定间隔重复运行多个命令

时间:2016-07-15 09:54:01

标签: bash command

我想使用脚本以一定的时间间隔重复运行多个命令。 我试过这个

----------------test_script---------------
while true;do 
ls -l >>output.txt
sleep 3
done


while true;do 
cat file.txt 
sleep 5
done

我想同时运行两个while循环。当我运行上面的脚本时,只有第一个while循环正在运行并且ls -l的输出被重定向到文件。如何同时执行两个while循环脚本

1 个答案:

答案 0 :(得分:1)

一种方法是在背景中运行其中一个循环,在下面运行其他循环。

#!/bin/bash

while true;do 
    ls -l >>output.txt
    sleep 3
done &             # Runs the first while loop in the background and passes to the next while loop

while true;do 
    cat file.txt 
    sleep 5
done