-1

I'm looking for a way to check if file content is not changed since 10 min and if not send a mail

File only contains 1 line with a number like this 1653014463445

edit: file "final" only contains the number to compare

curl --request GET --url "status.sensecapmx.cloud/api/openapi/device/…" > t123" 

save complete api call in file "t123"

&& cat t123 | tr ',' '\n' > neulines"

converts every "," to a new line and save it in a new file called "neulines" to be able to search the lines with grep (the wanted vale collectTime) which does the last part of the command

"&& grep -o 'collectTime.*' neulines | cut -f2- -d: > final" 

save only the value of collectTime in the new file "final"

(using Ubuntu 16.04.7 LTS)

5
  • How this number (1653014463445) is generated? Or it is static value? May 20, 2022 at 13:02
  • And how big is the file in question? May 20, 2022 at 13:04
  • @Romeo Ninov file is very small only content is the number, it get generated from an API call curl --request GET --url "status.sensecapmx.cloud/api/openapi/device/…" > t123 && cat t123 | tr ',' '\n' > neulines && grep -o 'collectTime.*' neulines | cut -f2- -d: > final its normally a "big" API answer i just "filter" the value of collectTime and that is the number to compare... if it does not changed the device is offline and thats why i want the mail to know about it
    – GiantJack
    May 20, 2022 at 13:08
  • 1
    I don't see how this is asking for a software recommendations.
    – Alejandro
    May 20, 2022 at 14:04
  • Welcome to Software Recommendations! Please note this site is about recommending software, not assets or resources like howtos, manuals/tutorials, best approaches, code fragments, etc. uestions like this one might fit on our ister site Stack Overflow(stackoverflow.com/help/on-topic` – but make sre to read their /help/on-topic page first (which all SE sites have).
    – Izzy
    May 21, 2022 at 12:36

1 Answer 1

0

You can try something like:

SUM=$(sha1sum <file>|awk '{print $1}')
while true
do
sleep 600
SUMNEW=$(sha1sum <file>|awk '{print $1}')
if [ "$SUM" = "$SUMNEW" ]
then
STR=$( curl --request GET --url "status.sensecapmx.cloud/api/openapi/device/…" > t123 && cat t123 | tr ',' '\n' > neulines && grep -o 'collectTime.*' neulines | cut -f2- -d: )
mailx <recipient> <<EOF
$STR
EOF
fi
SUM="$SUMNEW"
done

this script create infinite loop, get SHA1 sum, then after 10 minutes get it again, compare and send mail if they are not the same.

P.S. As I understand the file which we check is the result of curl. So the script may become something like:

curl --request GET --url "status.sensecapmx.cloud/api/openapi/device/…" > t123 && cat t123 | tr ',' '\n' > neulines && grep -o 'collectTime.*' neulines | cut -f2- -d: >final
SUM=$(sha1sum final|awk '{print $1}')
while true
do
sleep 600
curl --request GET --url "status.sensecapmx.cloud/api/openapi/device/…" > t123 && cat t123 | tr ',' '\n' > neulines && grep -o 'collectTime.*' neulines | cut -f2- -d: >final
SUMNEW=$(sha1sum final|awk '{print $1}')
if [ "$SUM" = "$SUMNEW" ]
then
mailx <recipient> <final
fi
SUM="$SUMNEW"
done

Feel free to define the curl call in function (if you want)

8
  • thank you very much that should work, but it should only send a mail if the value is the same because if the api call returns the same value the device is offline. also maybe its easy to use the file with the the number in it (called final here) and not use the whole curl command?
    – GiantJack
    May 20, 2022 at 13:21
  • @GiantJack, my mistake, will correct it to send mail when the file do not change :) ABout file or entire command - it's up to you. I will prefer variable because file an be deleted, changes, etc. May 20, 2022 at 13:25
  • @GiantJack, is it the name of the monitored file final (contain result of curl call)? May 20, 2022 at 13:27
  • 1
    i updated the question to be able to read it much better
    – GiantJack
    May 20, 2022 at 13:39
  • 1
    big thanks again it is working (device is offline now and i got a mail)
    – GiantJack
    May 20, 2022 at 14:17

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