Linux Process Monitoring

Yang Li
1 min readJan 6, 2020

--

Use the shell script below to run only one instance of a script, and if the process fail, restart it.

$ vi proc_mon.sh#!/bin/bash
#set -x
#Usage: proc_mon.sh <Unique Text to Grep> <command to start process>
grepText="${1}"
procStart=$2
logFile=/tmp/$1.log
echo "Text to grep to find process: "${grepText}
echo "Command to Start process : "${procStart}
echo "Log file location : ${logFile}"
nProc=`ps -ef|grep ${grepText}|grep -v grep|grep -v proc_mon.sh|awk '{print $2}'|wc -l`
echo ${nProc}
case ${nProc} in
0) echo "Starting : $(date)" >> ${logFile}
${procStart} &
;;
1) # all ok
;;
*) echo "Removed double proc: $(date)" >> ${logFile}
kill `ps -ef|grep ${grepText}|grep -v grep|grep -v proc_mon.sh|awk '{print $2}'|head -n 1`
;;
esac

Create below cron job to run the monitoring script every minute:

crontab -e* * * * * /<path to proc_mon>/proc_mon.sh start.js /<path to command>/<command>.sh > /tmp/proc_monitor.log 2>&1

--

--

No responses yet