How to make asynchronous function calls in shell scripts
Something to experiment with:
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
Related videos on Youtube
Harshit Laddha
I m a Web, Mobile & Cloud Developer based out of Bangalore, India. I love to code & develop awesome websites, apps.I am a generalist programmer with experience in multiple technologies and platforms. I have a flair for well-structured, readable, and maintainable applications and excellent knowledge of HTML, CSS, JavaScript, Python, PHP, Node.Js, SQL, MongoDB, Android (Java, Kotlin), iOS (Swift), React, Docker, AWS, GCP, etc.
Updated on January 04, 2020Comments
-
Harshit Laddha over 4 years
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
wait [sec]
command and execute different functions for different wait periods like
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?
-
Deleted User almost 10 yearsis 60 seconds resolution too course, which is why you don't run them as cron jobs?
-
-
Andreas Kalin almost 10 yearsAh, yes, cron is probably the right tool for this if minute resolution is sufficient.
-
Andreas Kalin almost 10 yearsAnd, similarily, the
at
command is useful if you want to run the command at a certain time rather than in certain intervals. Also minute resolution. Neithercron
, norat
requiresnohup
, which you may want if you use the scripted solution I hinted. A scripted solution will be killed by the HUP signal at logout unless started withnohup
(or disowned withdisown -h
or similar). -
Harshit Laddha almost 10 yearsThanks a lot, they did work for my purpose, only problem in future i might have is too many processes assigned for every timeout as i will be having a lot of them in near future
-
Andreas Kalin almost 10 yearsYou saw that I missed the ampersand first? Will not be async w/o it.
-
Andreas Kalin almost 10 yearsYes, bash tend to produce loads of processes. Since you're into node and js (and perhaps coffeescript) you could maybe consider doing it in node?
-
Harshit Laddha almost 10 yearsI know, i thought of finding some module to do it in node, but as it was needed urgently i though of implementing it in shell script first and then with node later