Skip to content

Instantly share code, notes, and snippets.

@themerius
Last active August 13, 2017 16:50
Show Gist options
  • Select an option

  • Save themerius/ade5f5c6e8f15bbed743dbb88d67e032 to your computer and use it in GitHub Desktop.

Select an option

Save themerius/ade5f5c6e8f15bbed743dbb88d67e032 to your computer and use it in GitHub Desktop.
Poor mans service monitoring implemented with crond.
# This cron-file should be deployed to crond:
# $ crontab cronic-monitoring
# Crond will start your services and checks for example every minute, if they are alive.
# Examples:
* * * * * bash -l -c "/abs/path/to/servicename_cronic-monitoring.sh config_a /abs/path/to/servicename_home"
* * * * * bash -l -c "/abs/path/to/servicename_cronic-monitoring.sh config_b /abs/path/to/servicename_home"
#!/bin/bash
# Arguments
MAIN=`basename "$0"`
CONF=$1
APP_HOME=$2
# Find all process IDs matching this scripts name
# Filter out the PID of this script instance itself (we don't want to see ourself)
pgrep -f "$MAIN.*$CONF" | grep -v ^$$\$ > /dev/null
# If we don't find any other instances of this program, the service is down and has to be started!
if [ $? != 0 ]
then
echo "[`date`] Starting $MAIN $CONF on `hostname`:$APP_HOME"
# Make the actual programm a child of this process
# Because of that, the "servicename" program should be blocking.
$APP_HOME/bin/servicename >> $APP_HOME/servicename.$CONF.log 2>&1
fi
#!/bin/bash
# Basic idea from
# https://www.digitalocean.com/community/tutorials/how-to-use-a-simple-bash-script-to-restart-server-programs
MAIN="servicename_cronic-monitoring.sh"
# Find the parent process
pgrep -f $MAIN > /dev/null
if [ $? == 0 ]
then
echo "[`date`] Current PID:"
pgrep -l -f $MAIN
echo "[`date`] $MAIN online"
else
echo "[`date`] $MAIN offline"
fi
#!/bin/bash
MAIN="servicename_cronic-monitoring.sh"
# Detect the parent pid / process group
PIDS=`pgrep -f $MAIN`
# Kill the process group
if [ ! -z "$PIDS" ]
then
echo "[`date`] Shutdown process group $MAIN"
echo "[`date`] Affected PIDs:"
for PID in $PIDS
do
# Prints all PIDs of the process group
pgrep -l -g $PID
# Shudown all those processes
pkill -g $PID
done
if [ $? == 0 ]
then
echo "[`date`] Shutdown sucess!"
else
echo "[`date`] Shutdown failed?"
fi
else
echo "[`date`] Nothing to stop"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment