连续运行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13368355/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:46:28  来源:igfitidea点击:

continuously run a bash script

bash

提问by David Vasandani

{ while IFS=';' read  u1 p1 u2 p2; do
            imapsync/imapsync --usecache --host1 secure.emailsrvr.com --user1 "$u1" --password1 "$p1" \
                     --host2 imap.gmail.com --ssl2 --user2 "$u2" --password2 "$p2" ...
     done ; } < users1-33.txt

How can I continuously run this script so that when it completes it just starts over again?

我怎样才能连续运行这个脚本,以便在它完成时重新开始?

回答by kojiro

Many UNIXy systems have a tool called watchthat will repeatedly execute a command (by default every 2 seconds). While watchis not a standard POSIX tool, if you have or can install it it is a very simple way to achieve essentially what you're asking for. Put your code in a file and watchit thus:

许多 UNIXy 系统都有一个称为watch重复执行命令的工具(默认情况下每 2 秒执行一次)。虽然watch它不是标准的 POSIX 工具,但如果您拥有或可以安装它,它是一种非常简单的方法,可以基本上实现您的要求。将您的代码放在一个文件中,然后观察它:

watch bash -c your_script

回答by Brian Cain

Try wrapping it in a function and iterating over it.

尝试将其包装在一个函数中并对其进行迭代。

function go()
{
    { while IFS=';' read  u1 p1 u2 p2; do
            imapsync/imapsync --usecache --host1 secure.emailsrvr.com --user1 "$u1" --password1 "$p1" \
                     --host2 imap.gmail.com --ssl2 --user2 "$u2" --password2 "$p2" ...
     done ; } < users1-33.txt

}

while true
do
    go
done

Or you can reference go()from another file:

或者您可以go()从另一个文件中引用:

   #!/bin/sh

   . /the/original/source_file.sh

   go

回答by matchew

have you considered putting the script in your crontab to run every minute?

你有没有考虑把脚本放在你的 crontab 中每分钟运行一次?

so on a debian based system. (ubuntu)

所以在基于 debian 的系统上。(Ubuntu)

crontab -e

crontab -e

# m h  dom mon dow   command
*/1 * * * * /usr/local/bin/scriptToRun.sh

would have the script run every minute

会让脚本每分钟运行一次

More on Cron

更多关于 Cron