Skip to content

Instantly share code, notes, and snippets.

@xonlly
Last active July 19, 2016 22:18
Show Gist options
  • Save xonlly/4fb5e13204402fdc7aa65f35e88d8bfe to your computer and use it in GitHub Desktop.
Save xonlly/4fb5e13204402fdc7aa65f35e88d8bfe to your computer and use it in GitHub Desktop.
#!/bin/bash
echo "Welcome to listen script"
echo "Check if process restart"
MJ_APIKEY_PUBLIC="*****"
MJ_APIKEY_PRIVATE="*****"
SENDER_EMAIL="contact@*****.com"
RECIPIENT_EMAIL="devteck@*****.com"
LOG_DIRECTORY="/var/log/*****.log"
function sendEmail {
LOGS=$(echo $(openssl enc -base64 <<< $(tail -n 100 $LOG_DIRECTORY)))
echo $LOGS
curl -s \
-X POST \
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
https://api.mailjet.com/v3/send \
-H "Content-Type: application/json" \
-d "{
\"FromEmail\":\"$SENDER_EMAIL\",
\"FromName\":\"Servers Tracktl\",
\"Recipients\": [
{
\"Email\":\"$RECIPIENT_EMAIL\"
}
],
\"Subject\":\"My first Mailjet Email!\",
\"Text-part\":\"Logs on attached file :D\",
\"Attachments\":[{\"Content-type\" : \"text/plain\", \"Filename\" : \"logs.log\", \"content\" : \"$LOGS\" }]
}"
}
LASTUPTIME=""
while [ true ]
do
PROCID=$(ps aux | grep "$1" | grep -v grep | grep -v listen | awk '{ print $2 }')
if [ "$PROCID" == "" ]
then
echo "Fail no process ID"
sleep 1s
continue
fi
UPTIME=$(stat -c %y /proc/$PROCID)
COMMAND=$(ps aux | grep $PROCID | awk '{ print $11 }')
if [ "$LASTUPTIME" != "" ] && [ "$LASTUPTIME" != "$UPTIME" ]
then
echo "This process has restarted"
echo "Send email to user"
sendEmail
fi
echo $PROCID
echo $UPTIME
echo $COMMAND
LASTUPTIME=$UPTIME
sleep 1s
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment