#!/bin/sh ####################################################################### #Script Name: queueprotect #Version: 1.5 #Description: HB queue protection script #Last Modify Date: 01062021 #Author:Brent Dacus #Email:brent[at]thedacus[dot]net ####################################################################### # Variables # ####################################################################### #Put your paths in here queue="/home/admin/domains/delainhosting.com/public_html/clients/queue.php" pidfile="/home/admin/hb_templates_c/hostbillqueue.pid" stampfile="/tmp/hbqtime.txt" lockdir="/tmp/hbqueue.lock" ####################################################################### # Main # ####################################################################### #set your php comand here. this starts the queue. #if the queue is already running it know and moves on. startqueue() { /usr/local/bin/php -q $queue } #this checks to see on the master hb queue is up and running. piddefunct() { pgrep -f 'hostbillqueue: master' >/dev/null 2>&1 if [ $? -eq 1 ]; then echo "Master process missing." pkill -sigterm -f hostbillqueue rm -f $pidfile fi } #checks for a pid number in the pid file. #if hb finds a file with or without a number it assumes all is well. pidfilemissing() { echo "Removing lock file." pkill -sigterm -f hostbillqueue rm -rf $lockdir } #checks for a pid number in the pid file. #if hb finds a file with or without a number it assumes all is well. pidfileempty() { echo "Removing empty file." pkill -sigterm -f hostbillqueue rm -f $pidfile } #checkes pid file is present and has a running master pid. #all is good.. pidfilenotempty() { pid=$(cat $pidfile) psck=$(ps -o pid= -p "$pid") >/dev/null 2>&1 if [ "$psck" -gt 0 ]; then echo "Hostbill master queue process already running." fi } #checks running master pid is more than 24 hours old. #kills and restarts. pidfileage() { touch -t "$(date -v-1d +%y%m%d0100.00)" $stampfile if find "$pidfile" -prune -newer "$stampfile" | grep -q '^'; then echo "Process file is newer than Timestamp file" else echo "Process is more than 24 hours old. Need new process." pkill -sigterm -f hostbillqueue rm -f $pidfile fi } if [ -f $pidfile ]; then if [ -s $pidfile ]; then echo "PID file present and not empty." pidfilenotempty pidfileage piddefunct else echo "PID missing from file.. bad ju ju." pidfileempty fi else echo "PID file missing." pidfilemissing fi if mkdir -p "$lockdir" >/dev/null 2>&1; then echo >&2 "Have acquired lock." # remove lockdir when the script finishes, or when it receives a signal trap 'rm -rf "$lockdir"' 0 # remove directory when script finishes startqueue else echo >&2 "Cannot acquire lock, lock exsits." exit 0 fi exit