Skip to content

Instantly share code, notes, and snippets.

@akashgoswami
Created July 2, 2018 08:43
Show Gist options
  • Save akashgoswami/3f656a43b5c51a8070f5bb7cb6999995 to your computer and use it in GitHub Desktop.
Save akashgoswami/3f656a43b5c51a8070f5bb7cb6999995 to your computer and use it in GitHub Desktop.
Bash script to wait for IOTIFY API
#!/bin/bash
# Provide your API Token
TOKEN=
# Provide the name of the template
TEMPLATE=hive
STATUS="pending"
#Invoke the run
RESULT=$(curl -X POST --silent -H "Content-Type: application/json" -H "X-Auth:$TOKEN" \
-d '{"template": "'$TEMPLATE'", "repeat": 5, "interval":10, "clients":1 }' \
https://api.iotify.io/api/v1/template)
JOB=$(echo $RESULT | jq '.job')
SUCCESS=$(echo $RESULT | jq '.success')
if [ "$SUCCESS" == "true" ];then
echo "Successfully launched template ${TEMPLATE} with job ID=${JOB}"
sleep 10
attempt_counter=0
max_attempts=100
while [[ "$STATUS" != *finished* ]]; do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "Max attempts reached"
exit 1
fi
attempt_counter=$(($attempt_counter+1))
sleep 10
STATUS=$(curl -X POST --silent -H "Content-Type: application/json" -H "X-Auth:$TOKEN" -d '{"job": '"$JOB"'}' https://api.iotify.io/api/v1/job | jq '.status.status')
printf .
done
sleep 10
STATUS=$(curl -X POST --silent -H "Content-Type: application/json" -H "X-Auth:$TOKEN" -d '{"job": '"$JOB"'}' https://api.iotify.io/api/v1/job)
echo "Finished with status ${STATUS}"
else
echo "Failed to launch template run. ${RESULT}"
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment