By Rex0226
My requirement is that i should be able to create a shell script which will check if a process is running on this machine and any other machines too i.e if i am trying to start a app on server3 it should first check if the app is already started on server1 and server2 if not then it should start this app on server3 else it should exit.
I tried using the script to create lock file to do the same but was not sure if that would, for an idea i will post that script here. please help me out guys, thank you!!!
CreateLockFile () {
unset PROCESS_ID
unset ID
LFILE=${TEMP_PATH}/${1}_${TODAYS_DATE}.LCK
ID=$2
if [ -r ${LFILE} ]
then
OPID=`cat ${LFILE}` 2> /dev/null
if [ -z ${OPID} -eq 0 ] #Make Sure OPID contains a value
then
exit ${FAILURE} “ERROR-APP–>: `basename ${LFILE}` exists but contains no
Process ID” | tee -a ${INLOG}
else
PROCESS_ID=`ps -p ${OPID} | grep ADD-SCRIPT-NAME-HERE | awk -F” ” ‘{print $1}’
2> /dev/null`
if [ ${PROCESS_ID} ] #Lock File is there, check if process is actually
running
then
echo “WARNING–>: ${1} Script Is Currently Running [PID=${OPID}], Exiting.
${DATE_TIME}” | tee -a ${INLOG}
exit ${SUCCESS}
else
echo “INFO–>: Old Lock File with PID= [ ${OPID} ] Exists But Process Is Not
Running. ” >> ${INLOG}
echo “INFO–>: Overwriting Old PID with New PID Value of [ ${ID} ] ” >>
${INLOG}
echo “$ID” > ${LFILE}
fi
fi
else
echo “$ID” > ${LFILE}
if [ $? -ne 0 ]
then
exit ${FAILURE} “ERROR-APP–>: Could Not Create Lock File – Exiting ” | tee -a
${INLOG}
fi
fi
}
…read more
Source: FULL ARTICLE at The UNIX and Linux Forums