Skip to content

Instantly share code, notes, and snippets.

@apzentral
Created July 6, 2019 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apzentral/80be76258579a4439cc092a19788d3b1 to your computer and use it in GitHub Desktop.
Save apzentral/80be76258579a4439cc092a19788d3b1 to your computer and use it in GitHub Desktop.
Bash: Script to Monitor Process. If not running will start it.
#!/bin/bash
####################
#
# Script: Checking process and if it is not running will start that process
#
####################
#*** CONFIG ***
CMD_PATH="/{Root Path to Your Script}" # Your Root Path to Script
declare -a PROCESSES=('{Your Script File Name}'); # Your Script File Name
PRINT_TITLE_SCRIPT="{Title}" # Your Script Title Here
#*** Vars ***
CHECK=$0
LOG_LOCATION=/dev/null
DATE=`date`
#*** Function ***
function monitor_process {
local OUTPUT=$(ps aux | grep -v grep | grep -v $CHECK |grep $1)
# Check the process if it is running?
if [ "${#OUTPUT}" -gt 0 ] ; then
# Server is running
echo " [*] Process ($1) is running."
else
# Service is not running, run the consumer process
echo " [x] Start Process ($1)"
$CMD_PATH$1 > $LOG_LOCATION &
# $CMD_PATH$1 &
fi
}
#*** Start ***
echo " *** Running - Start $PRINT_TITLE_SCRIPT ( $DATE ) ***"
for var in "${PROCESSES[@]}"
do
monitor_process ${var}
done
echo " *** Finished - $PRINT_TITLE_SCRIPT ***"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment