Find a process ID,kill it and restart agent


 
Thread Tools Search this Thread
Operating Systems Linux Find a process ID,kill it and restart agent
# 1  
Old 08-27-2015
Find a process ID,kill it and restart agent

Code:
#!/bin/bash
#This shell finds the pid of the hawkagent and kills and restarts to put the rulebase into effect
output=`ps aux|grep hawkagent`
#The set -- below helps to parse the above ps output into words and $2 gives the 2nd word which is pid
set -- $output
pid=$2
#Checks if pid of hawkagent exists then kills else starts the hawkagent and exits the program
if [[ $? -eq 0 ]]
then
{
        echo "Kill and start hawkagent if present"
        echo $pid
        kill $pid
        #Wait for 2 seconds for hawkagent to be killed else kill it forcefully if it still exists
        sleep 2
        kill -9 $pid > /dev/null 2>&1
        #Start the hawk agent after killing the existing pid
        nohup ./hawkagent_BWQA &
}
else
{
        echo "Start hawkagent if not present"
        #Start the hawk agent if the hawkagent is not available and exits the program
        nohup ./hawkagent_BWQA &
};
fi
exit

Objctive of the above process is to find a process-id and kill it and then restarts the agent again.
I have below 2 scenarios :
(a) If condition - works when a processID is already present and the process ID is killed and then the agent is restarted using following command :
Code:
nohup ./hawkagent_BWQA &

.
(b) Else condition- when the agent is not present then there is no processID. So this else part starts the agent and should exit.
However in this scenario the else part is not working . I believe my below if condition is not working properly
Code:
if [[ $? -eq 0 ]]

.
Any thoughts or suggestions is appreciated .
# 2  
Old 08-27-2015
Hi,

Code:
if [[ $? -eq 0 ]]

will not work since the the previous command:
Code:
pid=$2

will always render return code 0

Try this instead:
Code:
if [ $# -gt 0 ]

--
You could also use grep's return code:
Code:
if output=$(ps -ef | grep "h[a]wkagent")
then
  set -- $output
  pid=$2
  ...
else
  ...
fi

Or if your system contains pgrep, then you can just use:
Code:
if pid=$(pgrep hawkagent)
then 
  ...
else
  ...
fi


Last edited by Scrutinizer; 08-28-2015 at 04:20 AM.. Reason: Corrected with comments from MadeInGermany (post #5). Thanks, fine points..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-27-2015
You need to preserve the exit code of output=`ps aux|grep hawkagent` , e.g by adding a temporary variable immediately after the command: TMP=$? and checking that afterwards. Or try sth. like
Code:
if pid=$(ps axopid,comm | grep [h]awkagent)
  then echo "Kill and start hawkagent if present"
       kill ${pid% *}
  else echo "Start hawkagent if not present"
  fi
nohup ./hawkagent_BWQA &

This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-27-2015
Thanks .
I executed and it worked. Well when i run my code as ./test.sh i get the desired output but the nohup screen doesn't exit.
Code:
[BWQA]$ ./test.sh >> temp.txt
[BWQA]$ nohup: redirecting stderr to stdout

At some point i shall be calling this .sh file from my ant script.
# 5  
Old 08-28-2015
ps aux and ps -ef print the command arguments, so a grep needs the trick grep "[h]awkagent".
Note the quotes that prevent the shell from matching against files in the current directory.
pgrep does not need the trick.
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 08-28-2015
Quote:
Originally Posted by samrat dutta
. . . but the nohup screen doesn't exit. . . .
What do you mean?
# 7  
Old 08-28-2015
Hi I need to execute the entire line from ANT so i have reduced to a single line entry in ant.
How to combine the below two commands in a single line which finds the PID and also starts my hawkagent ?
Code:
kill $(ps axopid,comm | grep [h]awkagent | awk '{print $1}')

nohup ./hawkagent_BWQA &

I used the below . The agent is killed but the nohup is not starting the agent. Not sure if am missing something.

Code:
kill $(ps axopid,comm | grep [h]awkagent | awk '{print $1}') | nohup ./opt/tibco/tra/domain/BWQA/hawkagent_BWQA > /dev/null &


Last edited by samrat dutta; 08-28-2015 at 01:08 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find the Pid and Kill the Process after a Few Minutes

hi guys i had written a shell script Display Information of all the File Systems i want to find the pid and kill the process after few minutes.how can i obtain the pid and kill it??? sample.sh df -a >> /tmp/size.log and my cron to execute every minute every hour every day * *... (5 Replies)
Discussion started by: azherkn3
5 Replies

2. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

3. 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

4. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

5. Shell Programming and Scripting

Unix Script to find and kill a process with high memory utilization

Hi Unix Gurus i am somewhat new to unix scripting so need your help to create a script as below. # This script would find the process consuming memory beyond a certain #limit. if the meemory consumption is more than 100% for a period of 1 # minute for the specific process. the script would... (0 Replies)
Discussion started by: robinforlinux
0 Replies

6. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

7. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

8. Programming

kill(0,-9) don't kill the process

Hi all i have simple c program , when i wish to kill the app im using kill(0,-9) , but it seams this command don't do any thing and the program. just ignore it . what im doing wrong here ? im using HP-UX ia64 Thanks (9 Replies)
Discussion started by: umen
9 Replies

9. Shell Programming and Scripting

how to start a process and make it sleep for 5 mins and then kill that process

how to start a process and make it sleep for 5 mins and then kill that process (6 Replies)
Discussion started by: shrao
6 Replies

10. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

Hi All, I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages. How can i terminate this daemon ? ? ? Regards, Vijay Hegde (3 Replies)
Discussion started by: VijayHegde
3 Replies
Login or Register to Ask a Question