简体   繁体   English

是否有一种编写Bash脚本的方法,该脚本每隔X分钟反复运行一次?

[英]Is there a way to write a Bash Script, that repeatedly keeps on running every X minutes?

I'm making calls to an API, which is constantly fetching me data. 我正在调用一个API,该API不断获取我的数据。 There is a limited number of calls that I can make per 15 mins. 每15分钟可以拨打的电话数量有限。 Can I automate the whole process, and keep the script running for like 24 hours, which executes a certain command at the interval of 15 mins? 我是否可以使整个过程自动化,并使脚本运行24小时左右,以每15分钟的间隔执行某个命令?

Usual alternatives: 常用替代方法:

a) use "cron" to trigger the script periodically. a)使用“ cron”定期触发脚本。

b) finish the script with one "at" command, to reschedule it. b)用一个“ at”命令完成脚本,以重新计划它。

c) a never ending loop, as the loop in @VikasYadav answer. c)一个永无止境的循环,就像@VikasYadav答案中的循环。 However, this solution has the problem of stop working after system restart or script crash. 但是,此解决方案存在系统重新启动或脚本崩溃后停止工作的问题。 It should be complete with some "keep alive" configuration (/etc/inittab or similar). 它应该带有一些“保持活动”配置(/ etc / inittab或类似文件)。

You can use something like this: 您可以使用如下形式:

while(true)
   do
   # write your bash command to fetch API response using GET api_endpoint_url
   sleep 15m
   done

I hope this might serve the purpose! 我希望这可以达到目的!

Edit your crontab with the following command: 使用以下命令编辑crontab:

# crontab -e

Add the following line at the end of crontab: 在crontab的末尾添加以下行:

*/15 * * * * /path/to/your/command.sh

That will run your command every 15 minutes. 这将每15分钟运行一次命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM