Sponsored Content
Top Forums Shell Programming and Scripting Shell Script to find out if a process is running on any all machines Post 302770039 by Rex0226 on Thursday 14th of February 2013 08:43:17 AM
Old 02-14-2013
Shell Script to find out if a process is running on any all machines

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!!!

Code:
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
}

Moderator's Comments:
Mod Comment Use code tags please, thanks.

Last edited by zaxxon; 02-14-2013 at 10:09 AM.. Reason: code tags
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Running a script on multiple machines

To clear the web cache on my web server, I run this command: find $APACHE_HOME/cache/plsql/plsql -type d -name "*" -exec rm -R {} \; To clear the cache on all the web servers(we have 4), I log on to any one machine, clear its cache, ssh to another machine, clear cache etc; Is there any way... (8 Replies)
Discussion started by: nattynatty
8 Replies

2. Programming

to find current running process

Hi All, The scenario is like this: There is a process say "A" which create a child process say "B" if some condition is true and process "A" terminates. "B" invokes some C program say "C" using 'execl' function. The job of program "C" is to keep polling the server until the server will be up.... (2 Replies)
Discussion started by: ranjkuma692
2 Replies

3. UNIX for Advanced & Expert Users

how to check if a process is running in a server from shell script.

I want to write a unix shell script that will check if a process (say debu) is running in the server or not. If no , then send a mail to the corresponding person to start the process??? (2 Replies)
Discussion started by: debu
2 Replies

4. UNIX for Dummies Questions & Answers

How to find the details of the previously running process with PID

OS: Unix or Linux I (only) know the pid of the process which was running earlier (say 5 hrs back) but it is not running now. Is there a way I could find the details of that process? (atleast the name of the process). Please let me know. (2 Replies)
Discussion started by: vijay.d
2 Replies

5. Shell Programming and Scripting

script to monitor process running on server and posting a mail if any process is dead

Hello all, I would be happy if any one could help me with a shell script that would determine all the processes running on a Unix server and post a mail if any of the process is not running or aborted. Thanks in advance Regards, pradeep kulkarni. :mad: (13 Replies)
Discussion started by: pradeepmacha
13 Replies

6. Shell Programming and Scripting

Shell Script, Copy process using find.

Ok, so here is what I am looking for.. Shell script that uses find to look for one days worth of data using the modified date and then copies only those files to a specified directory. I figured I could use grep and the find command to do this. It seems to work just fine from what I can... (4 Replies)
Discussion started by: techjunky
4 Replies

7. Shell Programming and Scripting

Running Shell Script in the cron, background process

Hi, i was looking for an answer for some trouble im having runing a script in the cron, thing is, that when i run it manually it works just fine. But when cron runs it, it just doenst work. I saw a reply on a similar subject, suggesting that the . .profile worked for you, but im kind of... (9 Replies)
Discussion started by: blacksteel1988
9 Replies

8. Shell Programming and Scripting

shell script to find a process by name and kill it

hi, Am a newbie to unix and wasnt able to write script to my requirement. I need a shell script, which should find a process by name and kill it. For eg: let the process name be "abc". I have different processes running by this name(abc), so should kill them all. Condition would be: if... (7 Replies)
Discussion started by: fop4658
7 Replies

9. Solaris

Means to check if some process is running on "n" number of machines

Team, I would like to know, if we have any command in Solaris to verify, if some process is listening on a port on a set of machines. for eg: Wrote the below script, and found that when a process is listening on that port, then it just waits there and doesnt come out. Rather, I would like... (6 Replies)
Discussion started by: msgforsunil
6 Replies

10. Shell Programming and Scripting

Shell script to find out process name that are running for last 10 days.

Hi all, As i am new to shell script.Please help me to write a Shell script to find out process name that are running for last 10 days. Thank's in advance. (8 Replies)
Discussion started by: manas_1988
8 Replies
SHLOCK(1)						      General Commands Manual							 SHLOCK(1)

NAME
shlock - create lock files for use in shell scripts SYNOPSIS
shlock -p pid -f name [ -b ] [ -u ] [ -c ] DESCRIPTION
Shlock tries to create a lock file named name and write the process ID pid into it. If the file already exists, shlock will read the process ID from the file and test to see if the process is currently running. If the process exists, then the file will not be created. Shlock exits with a zero status if it was able to create the lock file, or non-zero if the file refers to currently-active process. OPTIONS
-b Process IDs are normally read and written in ASCII. If the ``-b'' flag is used, then they will be written as a binary int. For compatibility with other systems, the ``-u'' flag is accepted as a synonym for ``-b'' since binary locks are used by many UUCP pack- ages. -c If the ``-c'' flag is used, then shlock will not create a lock file, but will instead use the file to see if the lock is held by another program. If the lock is valid, the program will exit with a non-zero status; if the lock is not valid (i.e., invoking shlock without the flag would have succeeded), then the program will exit with a zero status. EXAMPLES
The following example shows how shlock would be used within a shell script: LOCK=/var/run/innd/LOCK.send trap 'rm -f ${LOCK} ; exit 1' 1 2 3 15 if shlock -p $$ -f ${LOCK} ; then # Do appropriate work else echo Locked by `cat ${LOCK}` fi HISTORY
Written by Rich $alz <rsalz@uunet.uu.net> after a description of HDB UUCP locking given by Peter Honeyman. This is revision 1.9, dated 1996/10/29. SHLOCK(1)
All times are GMT -4. The time now is 08:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy