如何在每N秒内重复运行bash脚本?

时间:2022-02-07 04:14:28

I have bash script with some statistic like disk info, memory use and so. But it shows information only once after script running and close. Can i make this script running repeatly ("htop" for example) or something like that? I want this info to update after 5-10 sec. Thanks!

我有bash脚本和一些统计数据,如磁盘信息、内存使用等。但它只显示了在脚本运行和关闭之后的信息。我可以让这个脚本反复运行(例如“htop”)或类似的东西吗?我希望这个信息在5-10秒后更新,谢谢!

3 个解决方案

#1


14  

A slight improvement to my comment: if your script exits with true (e.g. when it ends with exit 0), you can run

对我的评论有一点小小的改进:如果您的脚本以true退出(例如,当它以exit 0结束时),您可以运行

while script; do sleep 10; done

This is the canonical way to repeat a command as long as it doesn't fail.

这是重复命令的标准方法,只要它没有失败。

#2


15  

In linux you can use the watch program to repeat an action. Assuming that script.sh is executable:

在linux中,您可以使用watch程序来重复操作。假设脚本。sh可执行:

watch -n 10 path/to/script.sh

Would run it every 10 seconds.

每10秒钟运行一次。

To make your script executable, you can use chmod +x script.sh. Don't forget to add the shebang

要使脚本可执行,可以使用chmod +x script.sh。不要忘记添加shebang

#!/bin/bash

to the first line (assuming that it's a bash script).

到第一行(假设这是一个bash脚本)。

If you're running the script from your current directory, you can then do:

如果您正在从当前目录运行脚本,那么可以这样做:

watch -n 10 ./script.sh

#3


0  

As mentioned in a comment you need to wrap the script in an unconditional loop:

正如评论中提到的,您需要将脚本打包为一个无条件循环:

while :; do script; sleep 10; done

Depending on the script logic instead of the sleep 10 you may want to put different for instance the read - for the input data on the file/socket stdin. This will assure you script will not wake until some data to process will be present and if the data will be present the script will not take any time to wake.

根据脚本逻辑而不是sleep 10,您可能想要为文件/套接字stdin上的输入数据放置不同的内容,例如read -。这将确保您的脚本不会被唤醒,直到有一些数据出现,并且如果数据出现,脚本将不会花费任何时间来唤醒。

#1


14  

A slight improvement to my comment: if your script exits with true (e.g. when it ends with exit 0), you can run

对我的评论有一点小小的改进:如果您的脚本以true退出(例如,当它以exit 0结束时),您可以运行

while script; do sleep 10; done

This is the canonical way to repeat a command as long as it doesn't fail.

这是重复命令的标准方法,只要它没有失败。

#2


15  

In linux you can use the watch program to repeat an action. Assuming that script.sh is executable:

在linux中,您可以使用watch程序来重复操作。假设脚本。sh可执行:

watch -n 10 path/to/script.sh

Would run it every 10 seconds.

每10秒钟运行一次。

To make your script executable, you can use chmod +x script.sh. Don't forget to add the shebang

要使脚本可执行,可以使用chmod +x script.sh。不要忘记添加shebang

#!/bin/bash

to the first line (assuming that it's a bash script).

到第一行(假设这是一个bash脚本)。

If you're running the script from your current directory, you can then do:

如果您正在从当前目录运行脚本,那么可以这样做:

watch -n 10 ./script.sh

#3


0  

As mentioned in a comment you need to wrap the script in an unconditional loop:

正如评论中提到的,您需要将脚本打包为一个无条件循环:

while :; do script; sleep 10; done

Depending on the script logic instead of the sleep 10 you may want to put different for instance the read - for the input data on the file/socket stdin. This will assure you script will not wake until some data to process will be present and if the data will be present the script will not take any time to wake.

根据脚本逻辑而不是sleep 10,您可能想要为文件/套接字stdin上的输入数据放置不同的内容,例如read -。这将确保您的脚本不会被唤醒,直到有一些数据出现,并且如果数据出现,脚本将不会花费任何时间来唤醒。