implement a check to see if the script is already running

master
Daniel Jakots 6 years ago
parent 377f2eda46
commit d98d9643a8

@ -15,9 +15,20 @@ PATH=$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:.
_MAILTO="noc@example.com"
_TMPDIR=/tmp/check-bgp
_HOSTNAME=$(hostname)
mkdir -p "${_TMPDIR}"
# Don't try to run if it's already running
if [ -e "${_PIDFILE}" ]; then
echo "$(date)" >> "${_TMPDIR}"/log
# for debug purpose
echo "bgpd was already running" | mail -s "[BGP] ${_HOSTNAME}" foo@example.com
exit 1
else
echo $$ >> "${_PIDFILE}"
fi
if [[ -f "${_TMPDIR}"/bgp-status ]] ; then
mv "${_TMPDIR}"/bgp-status "${_TMPDIR}"/bgp-status.old
else
@ -45,7 +56,8 @@ done <"${_TMPDIR}"/peers-list
different=$(diff -q "${_TMPDIR}"/bgp-status.old "${_TMPDIR}"/bgp-status)
if ! [[ -n "${different}" ]] ; then
exit 0
rm -f "${_PIDFILE}"
exit 0
fi
# It changed so we're going to send a mail
@ -77,11 +89,10 @@ if [[ -f "${_TMPMAILDIR}"/bodynok ]] ; then
cat "${_TMPMAILDIR}"/bodynok >> "${_TMPMAILDIR}"/body
fi
_HOSTNAME=$(hostname)
cat "${_TMPMAILDIR}"/body | mail -s "[BGP] ${_HOSTNAME} - State change" "${_MAILTO}"
# cleaning
if [[ -d "${_TMPMAILDIR}" ]] ; then
rm -rf "${_TMPMAILDIR}"
fi
rm -f "${_PIDFILE}"

Loading…
Cancel
Save