DeirdreRodgers
DeirdreRodgers

Reputation: 441

How to write output of looped curl command to file

I want to run a curl command every few seconds on a loop and I want the output to be written to a file (appending not overwriting each time it runs). I have to also run it from the CLI

Curl command is like below

curl --location --request POST 'https://api.website.com/Auth/token'
--header 'Content-Type: application/x-www-form-urlencoded'
--header 'Cookie: blablabla'
--data-urlencode 'grant_type=password'
--data-urlencode 'username=username'
--data-urlencode 'password=password

I tried adding a while true; do sleep 2 && at the beginning and > file.txt to output at the end but its not working. I'm sure its probably simple but I'm not very good with bash so any help would be great

Upvotes: 0

Views: 2780

Answers (1)

konsolebox
konsolebox

Reputation: 75458

You can do this:

while :; do
    curl --location --request POST 'https://api.website.com/Auth/token' \
        --header 'Content-Type: application/x-www-form-urlencoded' \
        --header 'Cookie: blablabla' \
        --data-urlencode 'grant_type=password' \
        --data-urlencode 'username=username' \
        --data-urlencode 'password=password'
    sleep 2 || break
done >> file.txt

Upvotes: 1

Related Questions