Hi All, I have been a guest visitor from a long time and this forum is wonderful. I finally am a member of this forum too, so i am here stuck with a shell script that i was trying to write
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
}
Lock file is a bit of a misnomer here, since it doesn't lock anything. This is what's usually called a PID file. They often belong in /var/run.
Don't bother with ps | grep | awk | sed | cut | kitchen | sink games to check the name. That's not reliable and not portable, one oddly named process will break it, strange systems won't like it. If the PID file and the PID itself exist, trust it.
What's your system? If you're on a system that has /proc/, like Linux, that makes things a bit simpler.
If the script you're launching would be able to create and delete its own PID file, that would make it even more reliable.
I wrote this init script(some task-specific stuff stripped out) a little while ago:
#!/bin/bash
PIDFILE="/var/run/whatever.pid"
start() {
# If PID file exists, it can be read, the contents aren't blank, and the process still exists,
# it's already running.
if [ -f $PIDFILE ] && read PID < $PIDFILE && [ ! -z "$PID" ] && [ -d /proc/$PID ]
then
echo "Already started" >&2
exit 0
fi
echo "starting whatever..." >&2
nohup whatever & disown
echo "$!" > $PIDFILE
echo "started whatever" >&2
}
stop() {
if [ -f $PIDFILE ] && read PID < $PIDFILE && [ ! -z "$PID" ] && [ -d /proc/$PID ]
then
echo "stopping whatever"
# Give it a polite SIGINT
kill -INT $PID
sleep 1
# If it doesn't die, give it higher signals until it does
for SIG in QUIT TERM 9
do
[ -d /proc/$PID ] || break
kill -$SIG $PID
sleep 2
done
if [ -d /proc/$PID ]
then
echo "Couldn't stop whatever" >&2
return 1
fi
echo "stopped whatever" >&2
return 0
else
echo "whatever not started" >&2
exit 1
fi
}
case "$1" in
start) start ;;
stop) stop ;;
*) echo "unknown option" >&2 ; false ;;
esac
exit $?
I use it to start and stop a micro HTTP server. The script should work in any Bourne shell that has functions, including bash, ksh, dash, and busybox ash. If it complains about 'disown', remove it, some shells don't need that.
pgrep is a portable solution to "finding processes by name".
Invented on Solaris it came to Linux and now to HP-UX. man pgrep is the portable solution to get help on pgrep.