Skip to content

Instantly share code, notes, and snippets.

@backslash-f
Last active February 3, 2022 08:14
Show Gist options
  • Save backslash-f/bd655488201fe10f12493039115f026e to your computer and use it in GitHub Desktop.
Save backslash-f/bd655488201fe10f12493039115f026e to your computer and use it in GitHub Desktop.
Keeps Heroku servers awake
#!/bin/bash
# - Curls Heroku servers indicated in the "hosts" file.
# - Logs actions to log files (unique per host), including the response HTTP status
# - Clean up log files if > 5mb
#
# Simply add the target hosts in the "hosts" file, like:
# my-server-01.herokuapp.com
# my-server-02.herokuapp.com
# NOTICE: There must be a new line at the end of the "hosts" file.
#
# Remember:
# - To "chmod a+x heroku-pinger.sh"
# - On Mac:
# - Give "Full Disk Access" to "/usr/sbin/cron"
# - Give "Full Disk Access" to the "Terminal" app
#
# Cron job, every 20 minutes:
# */20 * * * * bash /Users/fernando.fernandes/Documents/Projects/Software/Scripts/Heroku/heroku-pinger.sh
cat /Users/fernando.fernandes/Documents/Projects/Software/Scripts/Heroku/hosts | while read hostname
# Loop over the list of hosts.
do
status=`curl -Is ${hostname} | grep HTTP | cut -d ' ' -f2`
d=`date +%Y-%m-%d`
t=`date +%I-%M-%S-%p`
day=`date +%A`
# Create the log file in the following format: "host-hostname..."
hostLogFilePrefix="host-"
hostLogFile="$hostLogFilePrefix${hostname}"
# Cleanup time! Check if the host log file exists.
wc -c $hostLogFile.log &>/dev/null
if [ $? -eq 0 ]
then
# If the file is greater than 5MB, clean it first.
fileSize=`wc -c $hostLogFile.log | awk '{print $1}'`
if [ $fileSize -gt 5242880 ]
then
:> $hostLogFile.log
fi
fi
# Save log to the host log file.
echo "$d $t $day $status" >> /Users/fernando.fernandes/Documents/Projects/Software/Scripts/Heroku/$hostLogFile.log
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment