如何在shell脚本异步函数调用 [英] How to make asynchronous function calls in shell scripts

查看:1231
本文介绍了如何在shell脚本异步函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本来执行卷曲的集合命令。现在,我想是所有这些命令在时间(每卷曲网址是不同的)以固定的间隔被执行所以我想要做的是使异步调用

I have a collection of curl commands to be executed by a shell script. Now what i want is all these commands have to be executed at a regular interval of time ( which is different for every curl url ) so what i want to do is make asynchronous calls to

等待[秒]

命令并执行不同的等待时期有不同的功能,如

command and execute different functions for different wait periods like

启动5个定时器一为120秒,2 30秒,3 3000S等。
然后,一旦他们得到完成我想要触发连接到每一个超时处理函数的执行。
我可以在JavaScript做到这一点,因为他们是事件驱动编程语言轻松的NodeJS。但我对shell脚本知之甚少。所以,我还能怎么实现这个或hotto使得在shell脚本,异步调用?
我不知道我是否足够清晰,我应该提什么其他细节,如果我不?

start 5 timers one for 120s, 2 for 30s, 3 for 3000s etc. and then as soon as they get completed i want to trigger the execution of the handler function attached to every timeout. I can do this in javascript and nodejs easily as they are event driven programming language. But i have little knowledge about shell scripting. So, how else can i implement this or hotto make such asynchronous calls in the shell script? I dont know if i am clear enough, what other details should i mention if i am not?

推荐答案

有得实验:

delayed_ajax() {
  local url=$1
  local callback=$2
  local seconds=$3

  sleep $seconds
  curl -s "$url" | "$callback"
}

my_handler() {
  # Read from stdin and do something.
  # E.g. just append to a file:
  cat >> /tmp/some_file.txt
}

for delay in 120 30 30 3000 3000; do
  delayed_ajax http://www.example.com/api/something my_handler $delay &
done

这篇关于如何在shell脚本异步函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆