The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
tracing processes in shell ukndoit Shell Programming and Scripting 1 03-04-2009 09:19 AM
Shell script creating too many processes. Miller_K Shell Programming and Scripting 3 05-22-2007 11:42 AM
need help: shell script to restart apache when no. of processes keeps growing _joshua_ Shell Programming and Scripting 14 03-07-2007 08:06 AM
Howto spawn multiple child processes and wait? siegfried High Level Programming 4 10-17-2005 01:21 AM
KORN Shell - Spawn new shell with commands frustrated1 Shell Programming and Scripting 2 04-20-2005 02:23 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 04-20-2009
sachin4sachi sachin4sachi is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 4
shell script for getting pid of spawn processes from shell

Hi,
I am new this forum. I request you peoples help in understanding and finding some solution to my problem.

Here it goes:

I need to perform this set of actions by writing a shell script. I need to read a config file for the bunch of processes to execute.
I need to fecth the pid of the executed process everytime i spawn it and put it to the background processing and continue to spawn other
process read from config file. Finally after spawing all process and put them for execution in background I need to wait for 10 minutes
and then kill them all based on their pid's gracefully. and restart the same process.

can you please let me know how to do this?
I will be very thankful if you can help me on this.

Example config file:
===================

PARAM1_MIN=20
PARAM1_MAX=40


PARAM2_MIN=20
PARAM2_MAX=30

PARAM3_MIN=1
PARAM3_MIN=100

....
...
...

CMD="lbench -a"" ( here i need to substitute all paremeters average of min and max )
And I need to spawn a process of CMD in background and get its pid for future to kill it gracefully

I will have some n number of such commands to be spawn.

example:- CMD1="lbench -a (PARAM1_MIN+PARAM1_MAX)/2 -z (PARAM2_MIN+PARAM2_MAX)/2 -t (PARAM3_MIN+PARAM3_MAX)/2"

I need to run such CMD's in background gettheir PID's and kill them gracefully after sleeping for 10 minutes.



please help me.
  #2 (permalink)  
Old 04-20-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 730
Well $$ will give you the script's PID, a ptree (Solaris ?) of that PID will list its parent and child processes, ps -ef | grep " <PID> " would list list all the direct child processes and the script itself in order to determine which PIDs you wish to kill.

HTH
  #3 (permalink)  
Old 04-21-2009
sachin4sachi sachin4sachi is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 4
Thanks TonyFullerMalv for your reply.

Yes using $$ I am able to get the pid of the current running process. Inside the script I have set of processes to be executed.
When I used '$!' to get the pid of the processes. I dont see any value in that.

could you please let me know how to collect the pid's of the executed commands or processes into a array. so dat using the index I can traverse through the pid list

Thanks for your reply
  #4 (permalink)  
Old 04-21-2009
TonyFullerMalv's Avatar
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Location: Malvern, Worcs. U.K.
Posts: 730
$! can be interpreted as run previous command again, depending on the shell, so is not PID related.

If I was killing off all the child processes I would do something like:
Code:
for PROC in `ps -ef | grep " $$ " | grep -v $0 | grep -v grep | awk '{ print $2 }'`; do
  if [ -n "${PROC}" -a ${PROC} -ne 1 ]; then
    kill $PROC
  fi
done
As far as getting child PIDs into an array I'll leave that for a more experienced scripting guru!
  #5 (permalink)  
Old 04-21-2009
sachin4sachi sachin4sachi is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 4
Quote:
Originally Posted by TonyFullerMalv View Post
$! can be interpreted as run previous command again, depending on the shell, so is not PID related.

If I was killing off all the child processes I would do something like:
Code:
for PROC in `ps -ef | grep " $$ " | grep -v $0 | grep -v grep | awk '{ print $2 }'`; do
  if [ -n "${PROC}" -a ${PROC} -ne 1 ]; then
    kill $PROC
  fi
done
As far as getting child PIDs into an array I'll leave that for a more experienced scripting guru!
Thanks for the solution TonyFullerMalv
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0