Home > OS >  How to write output of looped curl command to file
How to write output of looped curl command to file

Time:01-08

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

CodePudding user response:

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
  •  Tags:  
  • Related