Automated Script for Process killing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automated Script for Process killing
# 1  
Old 10-01-2015
Automated Script for Process killing

Hello all,

I'm in need of a Script which needs to wait for all the child process to end and then kill the main process.

I have a process called
Code:
mainpp

which runs for different instances like
Code:
evpn, nge, gmn etc

so when i query for
Code:
mainpp

process it looks like below.

Code:
bash-3.2$ ps -eaf | grep -i mainpp
   mainpp 14474 13672   0   mrt 08 ?          14:17 /opt/mainpp/bin/mon /opt/mainpp/etc/emtn-gsm.conf
   mainpp 14535 13672   0   mrt 08 ?          17:25 /opt/mainpp/bin/mon /opt/mainpp/etc/gmnjuniper.conf
   mainpp 14627 13672   0   mrt 08 ?           6:28 /opt/mainpp/bin/mon /opt/mainpp/etc/pwifi.conf
   mainpp 14636 13672   0   mrt 08 ?        1109:43 /opt/mainpp/bin/mon /opt/mainpp/etc/wwp.conf
   mainpp 14546 13672   0   mrt 08 ?          31:07 /opt/mainpp/bin/mon /opt/mainpp/etc/gmnwwp.conf
   mainpp 14440 13672   0   mrt 08 ?        1741:35 /opt/mainpp/bin/mon /opt/mainpp/etc/alcatel.conf
   mainpp 14447 13672   0   mrt 08 ?          13:18 /opt/mainpp/bin/mon /opt/mainpp/etc/asr9001.conf

the child process gets invoked for every 5 mins, and looks like below

Code:
bash-3.2$ ps -eaf | grep -i mainpp | grep -i rtr
   mainpp 17628 13672   0 11:10:08 ?           0:00 /opt/mainpp/bin/rtr poll.5 /opt/mainpp/etc/alcatel.conf
   mainpp 17213 13672   0 11:10:01 ?           0:00 /opt/mainpp/bin/rtr poll.3 /opt/mainpp/etc/os6250.conf
   mainpp 17487 13672   0 11:10:06 ?           0:00 /opt/mainpp/bin/rtr poll.10 /opt/mainpp/etc/eok.conf
   mainpp 17211 13672   0 11:10:01 ?           0:00 /opt/mainpp/bin/rtr poll.4 /opt/mainpp/etc/os6250.conf
   mainpp 17025 13672   0 11:10:01 ?           0:00 /opt/mainpp/bin/rtr poll.1 /opt/mainpp/etc/axxerra.conf
   mainpp 17503 13672   0 11:10:06 ?           0:00 /opt/mainpp/bin/rtr poll.0 /opt/mainpp/etc/wwp.conf
   mainpp 17217 13672   0 11:10:01 ?           0:00 /opt/mainpp/bin/rtr poll.1 /opt/mainpp/etc/os6250.conf
   mainpp 17470 13672   0 11:10:05 ?           0:00 /opt/mainpp/bin/rtr poll.4 /opt/mainpp/etc/wwp.conf
   mainpp 17493 13672   0 11:10:06 ?           0:00 /opt/mainpp/bin/rtr poll.7 /opt/mainpp/etc/eok.conf
   mainpp 17483 13672   0 11:10:06 ?           0:00 /opt/mainpp/bin/rtr poll.12 /opt/mainpp/etc/eok.conf
   mainpp 17626 13672   0 11:10:08 ?           0:00 /opt/mainpp/bin/rtr poll.6 /opt/mainpp/etc/alcatel.conf
   mainpp 17457 13672   0 11:10:05 ?           0:00 /opt/mainpp/bin/rtr poll.18 /opt/mainpp/etc/eok.conf
   mainpp 17467 13672   0 11:10:05 ?           0:00 /opt/mainpp/bin/rtr poll.13 /opt/mainpp/etc/eok.conf
   mainpp 17141 13672   0 11:10:01 ?           0:00 /opt/mainpp/bin/rtr poll.0 /opt/mainpp/etc/gmncisco.conf
   mainpp 17465 13672   0 11:10:05 ?           0:00 /opt/mainpp/bin/rtr poll.14 /opt/mainpp/etc/eok.conf
   mainpp 17640 13672   0 11:10:08 ?           0:00 /opt/mainpp/bin/rtr poll.2 /opt/mainpp/etc/alcatel.conf
   mainpp 17633 13672   0 11:10:08 ?           0:00 /opt/mainpp/bin/rtr poll.4 /opt/mainpp/etc/alcatel.conf
   mainpp 17463 13672   0 11:10:05 ?           0:00 /opt/mainpp/bin/rtr poll.15 /opt/mainpp/etc/eok.conf
   mainpp 17501 13672   0 11:10:06 ?           0:00 /opt/mainpp/bin/rtr poll.1 /opt/mainpp/etc/wwp.conf
   mainpp 17449 13672   0 11:10:05 ?           0:00 /opt/mainpp/bin/rtr poll.7 /opt/mainpp/etc/wwp.conf
   mainpp 17511 13672   0 11:10:06 ?           0:00 /opt/mainpp/bin/rtr poll.3 /opt/mainpp/etc/eok.conf
   mainpp 17635 13672   0 11:10:08 ?           0:00 /opt/mainpp/bin/rtr poll.3 /opt/mainpp/etc/alcatel.conf

after 5 mins, the
Code:
rtr

process gets ended on its own depends of work stack it has in its list, once the
Code:
rtr

is ended then i would like to kill
Code:
mainpp

i need a script which will wait for the
Code:
rtr

process to end and then kill the
Code:
mainpp

its just one time activity everyday, as i need to reboot the interfacing application and there is no particular time for the same so i need the script to trigger whenever i want.

Please help me with this.

Thanks!!
# 2  
Old 10-01-2015
Do you have pgrep|pkill?
Code:
#!/bin/sh
# wait for the rtr processes to terminate
while pgrep -u mainpp -x rtr
do
  sleep 5
done > /dev/null
# terminate the mon processes
pkill -u mainpp -x mon

Otherwise please state your OS
Code:
uname

# 3  
Old 10-01-2015
Automated Script for Process killing

Hello,

Thanks for the solution!!

its not always 5 Sec that rtr takes to complete, its always work stack in its memory that matters.

Apparently, the below script only waits for 5 sec and kill the mainpp abruptly, which i dont want to do

I have written below script to my knowledge

Code:
#!/bin/bash
# Version 1.0
# Script to kill all mainpp processes

HOSTNAME=`hostname`
echo $HOSTNAME
BASEDIR=/opt/mainpp
PROCESS_DATA=/opt/mainpp/log/automation_scripts/logs/mainpp_kill_`date +%d%m%Y`
dat=`date +%d%m%Y`
chmod 755 `echo $0`
echo $dat
flag=5

rtr_count=`ps -eaf | grep mainpp | grep -i rtr | grep -v grep | wc -l`

while [ $flag -eq 5 ]
do
  #sleep 30
	if [ $rtr_count -eq 0 ]
	then
		echo "no rtr process is running, mainpp will be killed!! `date`" >> $PROCESS_DATA
		PID=`ps -eaf | grep mainpp | grep -v grep | awk '{print $2}'`
			if [[ "" !=  "$PID" ]]; then
				echo "killing $PID" >> $PROCESS_DATA
			kill $PID
		fi
		break 
	else 
		sleep 30		
	 
    fi
done
exit


Can you please let me know if there are any flaws before i execute the same in prod??

Thanks,

Last edited by Mahesh_RPM; 10-01-2015 at 11:09 AM..
# 4  
Old 10-01-2015
Quote:
Originally Posted by Mahesh_RPM
Can you please let me know if there are any flaws before i execute the same in prod??
I would think that this is why people have dev environments.
You might want to find a dev environment and try it there first.
Smilie
# 5  
Old 10-01-2015
Smilie Thats true, unfortunately its a standalone system with no dev or acceptance env Smilie
# 6  
Old 10-01-2015
Quote:
Originally Posted by Mahesh_RPM
Smilie Thats true, unfortunately its a standalone system with no dev or acceptance env Smilie
Try changing to send an email when it thinks the child processes
are finished. and you can check and see if you agree. Once you
get that working you can change it to find and kill the right
process. You should have the email include everything that you
would need to kill the parent process, then you will know that
the script would have killed the right process.
# 7  
Old 10-01-2015
Your script has an unsharp grep.
For example it will also kill a process /data/domainppd because it contains mainpp.
And what is chmod 755 `echo $0` for??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing the process if running for long time in script

I am running a script which will read the data from fail line by line and call the Java program by providing the arguments from the each line. The Java code is working fast for few records and for some records its getting hanged not providing response for morethan one hour. Currently am... (4 Replies)
Discussion started by: dineshaila
4 Replies

2. Shell Programming and Scripting

Killing a bash process and running the second part of script

I want to run a script that calls remote ssh and if it gets hung, I want to be able to kill that part of the script and run another command in the script for example I have a statement like this: if ]; then export tapes=$(for tape in $(su - nacct -c 'ssh remote1 "cat... (1 Reply)
Discussion started by: newbie2010
1 Replies

3. Shell Programming and Scripting

script killing a process from service status output

Hello, can some please suggest a script, for killing the process PID. This are steps I am currently performing to kill the process. I cant user service splunk stop, to kill these processes, because of uid and gid mismatch for splunk user. # service splunk status Splunk status: splunkd... (8 Replies)
Discussion started by: bobby320
8 Replies

4. Homework & Coursework Questions

Unix Script -- To Skip killing a specific process

Hi, By using ps -aux | awk '/mine/{split($15,a,"/");print $1,$2,a}' i get the below listed PID's with there corresponding processes. adm 1522 ABC_Process.tra adm 1939 GENE_Process.tra adm 2729 GENE_Archive.tra adm 3259 xyz_Process.tra I use ps -aux | awk... (5 Replies)
Discussion started by: murali1687
5 Replies

5. Shell Programming and Scripting

**need help for killing a process through script**

Hello All, i hope you are fine. I need a little help from you people-- inside a script i want to kill a parent process by checking it with the child process.. p_pid=`ps -e | awk '/ra_cmd_d/ {print$1}'` here i am selecting the child process id in p_pid. next-- sleep_pid=`ps -af |... (3 Replies)
Discussion started by: onlyniladri
3 Replies

6. Shell Programming and Scripting

script not killing process!

i am using script to connect remotly to server and run some commands , one of these commands is to kill some process but tried different ways with no hope sshpass -p 'pass' ssh -o StrictHostKeyChecking=no server kill -9 `pgrep procs` getting error message "kill: bad argument count" ... (2 Replies)
Discussion started by: mogabr
2 Replies

7. Shell Programming and Scripting

While killing the process, Script gets hanged. Please help me on this.

Hi Everyone, I have written a script to automate the docbase cleanup process on every weekly basis. That is in this script i have to shutdown the docbase and then kill all the process that are hanged except one process(epic process) and need to delete some log files and then i have to start the... (8 Replies)
Discussion started by: Sheethal
8 Replies

8. AIX

killing a process from a script

Hey all. I'm brand new to this forum and am looking for some help. I have a script that verifies that the backup tapes are working correctly. Basically is uses 1 command: restore -xpqvf > rootvglog I use this for each volume group that we have. We run this everyday but the problem is, we... (4 Replies)
Discussion started by: jalge2
4 Replies

9. Shell Programming and Scripting

Killing a process from perl script.

How do I kill a process, say by name "drec" from a perl script. I tried with : ps -eaf | grep drec | awk '{print $2}' | xargs kill -9. The output I got is : ps -eaf | grep drec | awk '{print }' | xargs kill -9 /usr/bin/kill: ipgen: Arguments must be %job or process ids {But, $2 is not... (3 Replies)
Discussion started by: sharuvman
3 Replies

10. Shell Programming and Scripting

killing process using a script

can I do ps -ef | grep <process_name> and kill the process is it exists? and send a mail to me that the process was found and killed Thanks much... KS (4 Replies)
Discussion started by: skotapal
4 Replies
Login or Register to Ask a Question