How to make asynchronous function calls in shell scripts

22,416

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
Share:
22,416

Related videos on Youtube

Harshit Laddha
Author by

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, 2020

Comments

  • Harshit Laddha
    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
      Deleted User almost 10 years
      is 60 seconds resolution too course, which is why you don't run them as cron jobs?
  • Andreas Kalin
    Andreas Kalin almost 10 years
    Ah, yes, cron is probably the right tool for this if minute resolution is sufficient.
  • Andreas Kalin
    Andreas Kalin almost 10 years
    And, 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. Neither cron, nor at requires nohup, 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 with nohup (or disowned with disown -h or similar).
  • Harshit Laddha
    Harshit Laddha almost 10 years
    Thanks 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
    Andreas Kalin almost 10 years
    You saw that I missed the ampersand first? Will not be async w/o it.
  • Andreas Kalin
    Andreas Kalin almost 10 years
    Yes, 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
    Harshit Laddha almost 10 years
    I 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