LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-10-2010, 12:42 AM   #1
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Rep: Reputation: 0
capturing PID and killing the process


Hi, I want to capture PID of a program and kill it using the PID if the program is been executing beyond 5 seconds. Below is my shell script.

#!/bin/sh

PROGRAM='TESTPGM1'

./TESTPGM1 > OUT &

# give some time to PROGRAM to execute
sleep 5

# Capture the PID of the program
PID=`pgrep $PROGRAM`

# If the program is still running, kill it
if ps $PID > /dev/null
then
echo "$PROGRAM is still running, going to kill PID $PID"
kill $PID
fi

This works perfectly. The problem I'm facing is I have another copy of the same program running under different shell script, and the above code is killing both the process. How do I specifically kill the program which has started under the current running shell script.
 
Old 10-10-2010, 12:50 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
The special var $! contains the pid of the last backgrounded process in the current shell, so you can capture it :
Code:
./prog &
pid=$!
.
.
.
.
kill $pid
 
Old 10-10-2010, 01:00 AM   #3
poojithas
LQ Newbie
 
Registered: Jun 2010
Location: Chennai, India
Distribution: Ubuntu 10.04
Posts: 22

Original Poster
Rep: Reputation: 0
That works.
Thank you very much Chris.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the PID of the process giving kill signal to a process? hariprd Programming 2 11-27-2008 04:10 AM
perl script for capturing Memory used by each process Sherlock Programming 3 02-27-2007 11:52 PM
Killing a pid in C++ pilot1 Programming 4 09-20-2003 11:07 AM
killing tail --pid=??? DavidPhillips Linux - Software 1 08-13-2003 11:48 PM
finding a PID and killing it tangle Linux - General 3 01-25-2003 11:14 PM


All times are GMT -5. The time now is 04:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration