Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the PID

Status
Not open for further replies.

crystal28

Programmer
Oct 23, 2002
108
US
I am new to Shell Scripting so can anyone give me the syntax in C Shell for getting a process id inside a shell script after every process is done.

i tried the below and it did not give me the pid
<set of processes>

set v_pid=ps

echo $v_pid
 
Well, two things:

First, you can't get the process id of a process that's complete, as it goes away.

Second, ps is a command that given the right flags returns more than just a single number - it returns a lot of data. You have to parse it to get the specific numbers you want.

Try running &quot;ps&quot; from your command line, and depending on your flavor of unix, &quot;ps -ef&quot; or &quot;ps -aux&quot; for more output.
 
Thanks for the tip.I am running c shell and in that i am getting the pid in a varible x thru the below statements

foreach x (`ps -o ppid | cut -c2-8`)

inside this loop i want to skip the first word of the string that x has i.e PPID is not i want as i want to kill the processes..Please let me know how to do this.
end
 
Hi,

use $$ for your scripts pid.

set ppid = $$

set PIDS = ( ` ps -o $ppid | egrep -v PPID | cut -c2-8` )

will obtain a list of all your child processes. don't forget the parentahsis in case it returns more than 1 value.



My CSH KILLJOBS script goes like this. I let the user ( me ) Pass in a string to search processes for to kill.

I am a little paranoid about killing more than one job in any run of the script. I guess I could use ps -ef but I would have to count the column ( or maybe 2-8 is correct ).


#! /bin/csh

set PIDS = ( `ps -auxw | egrep &quot;$1&quot; | egrep -v egrep | cut -c10-15` )

if ( $#PIDS == 0 ) then
echo &quot;No Processes selected to kill&quot;
exit 5
endif

if ( $#PIDS > 1 ) then
echo &quot;More than one process selected&quot;
echo &quot;Are you sure you want to kill these $#PIDS tasks?&quot;
set a = $<
if ( &quot;$a&quot; != &quot;YES#&quot; ) then
echo &quot;$0 aborted without killing jobs&quot;
exit 5
endif
endif

nohup kill -9 $PIDS

#see if they died.

ps -p &quot;$PIDS&quot;





 
prouve you readed once again 'man ps' and realized:
ps -ef|grep something|grep -v grep|awk OR cut OR sed'
was perfectly correct ... 10 years ago.
(sys5) ps has better options:

NOTA: interprete ' as a back quote and Q as a space


#!/bin/sh
WHAT_YOU_WANT=xxx
WHAT_YOU_GET='/bin/ps -efocomm,pid|sed -en &quot;s/.*$WHAT_YOU_WANT.*Q//p&quot;'
[ x$WHAT_YOU_GET = x ] && echo $WHAT_YOU_WANT not found && exit 1
kill 0 $WHAT_YOU_GET || exit 1
kill -9 $WHAT_YOU_GET
exit 0 -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top