file st.txt:

"succeeded" "test" "2018-03-30T13:42:12Z" "2018-03-30T13:42:21Z"

i have a script which returns latest Rundeckjob status

curl -s -H "Accept: application/json" -X GET "http://localhost:4440/api/20/project/Windows-AD/executions?authtoken=r50onM4kXzZ7DhhAN6Fe2rwwxuAhF0IG"  | python -m json.tool > 1.json

jq -r '.|[.executions[] | select(.job.name != null)]  | sort_by(.id)  | reverse  | .[0]  | [.status, .job.name, ."date-started".date, ."date-ended".date] | @csv' 1.json > 1.csv

sed 's/,/ /g' 1.csv>st.txt
while read status name startdate enddate; do
case "$status" in
echo -1
echo -1 +  $name
echo 0
*) echo 0 + $name
exit 0

How to compare $start and $enddate ("2018-03-30T13:42:12Z" "2018-03-30T13:42:21Z") and if difference is larger than 5 minutes add another exit "taking too long"


you can convert and compare start/end dates like this:

stime=$(date -d "${startdate/T/ }" +%s)
etime=$(date -d "${enddate/T/ }" +%s)
let elapsed=etime-stime
[ $elapsed -gt 300 ] && echo "taking too long"
  • 1
    There is no need to remove the T for GNU date. – Isaac Apr 5 '18 at 19:39
  • @isaac, not with ast-open date either. With busybox date, one would need to remove both the T and Z and add the -u option. I don't know of any other date implementation that supports that GNU-style -d option. – Stéphane Chazelas Apr 15 '18 at 14:10
  • @StéphaneChazelas With busybox date, try this: busybox date -d '2018-03-30T13:42:12Z' -D '%Y-%m-%dT%H:%M:%SZ'..... It raises no error. ... I shall concede that it does not understand that Z means Zulu time and that it also means UTC, i.e. the -u flag is useful in this case to have the resulting time zone as UTC. – Isaac Apr 15 '18 at 15:41

GNU date could do the math directly (in seconds).
POSIX script.



sd=${sd#\"}; sd=${sd%\"}    # Remove double quotes.
ed=${ed#\"}; ed=${ed%\"}

ep=$(date -d @0)              # Epoch date (anchor).
ss=$(date -d "$sd" +%s)       # Start time.
es=$(date -d "$ed" +%s)       # End time.
elapsed=$(date -d "$ep - ${ss}sec + ${es}sec" +%s)

if [ "$elapsed" -gt 300 ]]; then 
    echo "taking too long"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.