How to Run a cURL Command Every N Seconds


How can we run a cURL command every n seconds?

Suppose we want to run the following cURL command every 2 seconds.

curl -d "index=0" -X POST https://example.com

However, let’s also increment the index on every cURL command.

Check out this article to learn more about POST requests with cURL.

Let’s create a bash script to achieve this functionality.

#!/bin/bash

INDEX=0

while sleep 2
do
	curl -d "index=$INDEX" -X POST https://example.com
	((INDEX++))
done

We can start our loop with while sleep 2, which will run the do block every 2 seconds.

We’ll pass the INDEX variable into our -d argument using $INDEX.

Then, we’ll increment that variable using ((INDEX++)).

If we want to send the cURL commands as quickly as possible, we can simply run the loop while true.