shell script for getting pid of spawn processes from shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script for getting pid of spawn processes from shell
# 1  
Old 04-20-2009
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  
Old 04-20-2009
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  
Old 04-21-2009
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  
Old 04-21-2009
$! 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  
Old 04-21-2009
Quote:
Originally Posted by TonyFullerMalv
$! 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

2. Shell Programming and Scripting

Help with shell script handling processes

Hello I have a file which has around 120 lines of commands. I am trying to write a shell script like which reads the 'command' file and executes line by line with some additional (common argument) with maximum 6 commands active at a time. Each of these commands when executed takes time... (5 Replies)
Discussion started by: JackyShane_36
5 Replies

3. Shell Programming and Scripting

Wait for one processes to complete in a shell script

Let's say I start process A.sh, then start process B.sh. I call both of them in my C.sh How can I make sure that B starts its execution only after A.sh finishes. I have to do this in loop.Execution time of A.sh may vary everytime. It is a parameterized script. (17 Replies)
Discussion started by: rafa_fed2
17 Replies

4. Shell Programming and Scripting

Spawn sftp and Shell commands

Hi everyone, I'm no killer in shell scripting, that is why I've searched and found a little script that explained how to do what I wanted to do : a FTP transfer from distant servers. I adapted it cause as such, it didn't work. As I needed to do some very simple shell commands (erase and... (2 Replies)
Discussion started by: mederik
2 Replies

5. Shell Programming and Scripting

Help with spawn.. newbie to shell

Hi, I have a problem with the spawn execution with expect.. i have done the code for expect in a separate file and i am calling the this execution from the bash script.. as given below.. -bash-4.1$ cat main.sh #!/usr/bin/bash ./spawn.exp ========================== -bash-4.1$ cat... (2 Replies)
Discussion started by: satishkumar432
2 Replies

6. UNIX for Dummies Questions & Answers

Bash: Regulating the number of processes a script can spawn

I've been working on some scripts in which I spawn some background processes. I'd like to be able to limit the number of processes, but have my script spawn additional processes as previous tasks finish. So, let's say I have 20 tasks to complete. Any given task could take from 1 to 10 minutes. ... (7 Replies)
Discussion started by: treesloth
7 Replies

7. AIX

Having problem with executing shell script to get the pid details!

Hello Everyone, I am new to shell scripting and also i am very new to AIX machine I have a question. I have shell script which contains the follwing "ps -e |grep $1 echo $2" i run this schell script by doing this ./startSehllscript 3444 passed with two parameters . Accroiding my... (4 Replies)
Discussion started by: swati
4 Replies

8. Shell Programming and Scripting

Shell script creating too many processes.

I have a shell script that I am running every 60 seconds, but it is creating this process to the point that it is causing the server to perfrom poorly. Below is my script, what can I change to prevent this? while true do java -classpath .....( all my classes here) >/dev/null 2>&1 ... (3 Replies)
Discussion started by: Miller_K
3 Replies

9. Shell Programming and Scripting

Need to know rhe PID for the Shell Script running

I have a Shell Scritp named "Statistics" which has a Infinate Wille Loop Running I want to restart this Srcipt "Statistics" when i try to run other srcipt Ex "ABC" so i want to kill the "Statistics" script in "ABC" so for this I what to know the PID for that Script "Statistics" which is... (7 Replies)
Discussion started by: pbsrinivas
7 Replies

10. Shell Programming and Scripting

KORN Shell - Spawn new shell with commands

I want to be able to run a script on one server, that will spawn another shell which runs some commands on another server.. I have seen some code that may help - but I cant get it working as below: spawn /usr/bin/ksh send "telnet x <port_no>\r" expect "Enter command: " send "LOGIN:x:x;... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question

Featured Tech Videos