Last active
February 19, 2021 17:19
-
-
Save codarrenvelvindron/869f841a5326f92dbbe32987bbff4c2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Check modified date of file and return the time elapsed between modification and current time | |
#Limiting API curl requests with bash | |
#By Codarren Velvindron | |
#Credits: https://stackoverflow.com/questions/1819187/test-a-file-date-with-bash | |
CWD="$( cd "$(dirname "$0")" ; pwd -P )" | |
file="$CWD/myfile.txt" | |
API_Request='curl -G -H "your_api_key:xxxxxxxxxxxx" https://your.api.url/your/endpoint -o $file' | |
threshold='100' | |
NOW=$(date +%s) | |
MOD=$(date --reference="$file" +%s) | |
echo "#Checking file at location $file" | |
delta=$(($NOW-$MOD)) | |
echo "--> $file was modified $delta secs ago" | |
if [[ $delta -lt $threshold ]]; then | |
echo "--> File didnt exceed threshold-$threshold, exiting" | |
elif [[ $delta -ge $threshold ]]; then | |
echo "--> File exceeded threshold-$threshold, Launch the request!" | |
$API_Request | |
else | |
echo "!! EXCEPTION caught!" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment