Visit Our UNIX and Linux User Community

Another PID ?

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Another PID ?
# 1  
Old 08-29-2008
Tools Another PID ?

I have searched to find an anwer to no avail, I hope you can help me.
I have a.ksh that many people call and a.ksh calls b.ksh

b.ksh is also invoked stand-alone by many people as well

In b.ksh I want to do something different if it was not involked by a.ksh.

How can I do this?
# 2  
Old 08-29-2008
Believe you can check the following vars..

$$ = The PID number of the process executing (this might return the PID of that script not the parent, I dont remember)

I havnt tried it, but I think it would work.


you can call it from a.ksh and pass a param to b.ksh and check for that param

Here is how to get the Parent id:
# 3  
Old 09-02-2008
So, I guess the PID and the PPID are always going to be different. I thought I could tell if someone had launched b.ksh directly or if b.ksh was called from a.ksh. I think I will need to check the name of the program for the PPID and compare it to a local variable in b.ksh. Does anyone know how to get the program name for the PPID on Unix Solaris?
# 4  
Old 09-02-2008
Can someone please help me?
I have the following command that gets me what I need on the command line
ps -ef| awk '$2 == 9999 {print $8}' # where 9999 is my current sessions PPID

When I try to put it into my Korn shell it does not behave the same way:
my_var=`ps -ef| awk '$2 == $PPID {print $8}'`
# 5  
Old 09-02-2008
You need to fix the quoting, the $PPID is not getting expanded inside single quotes.

my_var=`ps -ef | awk '$2 == '"$PPID"' { print $8 }'`

Maybe there's an option to ps to say which PIDs you want; it's available on Linux at least.
# 6  
Old 09-02-2008
Thank you so much, that worked, I really appreciate your help.
# 7  
Old 09-02-2008
Many awk implementations also allow passing shell variables, either via the -v option, or as trailing argument (after the file to parse).
So you could probably avoid some quoting trouble by e.g.
Since you are on Solaris the awk there knowing this option might be in /usr/xpg4/bin
while the /usr/bin awk could be a little more dumb.
$ ps -ef | awk -v ppid=$PPID '$2==ppid {print $8}'

But I think you could further improve by setting up your own format instead of relying on -f
which may change between different ps implementations.
Just tell ps what fields you are interested in.
cmd=`ps -e -o ppid= -o comm= | awk -v ppid=$PPID '$1==ppid {print $2}'`

Note, that this would only capture the command.
If you need the command line including arguments substitute -o comm= by -o args=.
Unfortunately, I haven't access to a Solaris box right now, and I don't know by heart.
So I may be wrong, but I think to remember that Solaris also has a pgrep command similar to that found on Linux (I would suggest to refer to man pgrep)
Another, often working way (except in C-shell compatibles (well except on FreeBSD yet where it even works in csh)) is to simply refer to $0 for the current shell, and $- for its arguments.
Then you also might be interested to note that Solaris has a ptree command
similar to pstree on Linux, which will list a process hierarchy tree that could be parsed.
And Solaris even has a rudimentary procfs with a set of accompanying p* commands (like ptree above).
As said, I am only recapitulating this without a system to verify at hands.
Therefore, please check the man pages.

Previous Thread | Next Thread
Test Your Knowledge in Computers #945
Difficulty: Medium
A Fraggle attack is a variation of the Smurf attack where an attacker sends a large amount of UDP traffic to ports 7 (Echo) and 19 (CHARGEN) to an IP broadcast address with the intended victim's spoofed source IP address.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting


Hello, I would like to know what this command means? PID=$! what does "!" stand for? (5 Replies)
Discussion started by: googlietdr
5 Replies

2. UNIX for Dummies Questions & Answers

PID value

name the process wit PID value 0? swapper or scheduler or both.... (3 Replies)
Discussion started by: karthi_g
3 Replies

3. UNIX for Dummies Questions & Answers

Get pid

Hello people, This question might seem to be a little naive but here it goes: I want to know the PID of a script that is running in the background. eg: There is a script called I am using the command: ps -ef | grep But I am getting the output as wrkarea... (9 Replies)
Discussion started by: Rajat
9 Replies

4. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

5. Shell Programming and Scripting

KILL PID, intern should kill another PID.

Hi All, In my project i have two process runs in the back end. Once i start my project, and execute the command ps, i get below output: PID TTY TIME CMD 9086 pts/1 0:00 ksh 9241 pts/1 0:02 java 9240 pts/1 0:00 shell_script_bg java with 9241 PID is the main... (4 Replies)
Discussion started by: rkrgarlapati
4 Replies

6. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies

7. UNIX for Dummies Questions & Answers

getting PID

Hi , I am trying to get the PID using the following command: $ /usr/ucb/ps -auwwwwx | grep java | grep Proceess | ptree PID or $ /usr/ucb/ps -auwwwwx | grep java | grep Proceess;ptree PID it is possible to get PID, such that I check whether any orphan process is running. solution... (0 Replies)
Discussion started by: Rakesh Bhat
0 Replies

8. Shell Programming and Scripting

how to get PID only

The below command returns full line.How can i get only PID from this line ie 15794 from the below example (FI NY) ->ps -ef | grep keepalive | grep -v keepaliveStub | grep -v swapback | grep -v grep ficctprd 15794 1 0 13:12:58 ? 0:01 keepalive (3 Replies)
Discussion started by: kotasateesh
3 Replies

9. UNIX for Advanced & Expert Users


What OS does not use PID=0? is it FreeBSD? (0 Replies)
Discussion started by: nmajin
0 Replies

10. Programming

printing ppid,child pid,pid

question: for the below program i just printed the value for pid, child pid and parent pid why does it give me 6 values? i assume ppid is 28086 but can't figure out why there are 5 values printed instead of just two! can someone comment on that! #include<stdio.h> #define DIM 8 int... (3 Replies)
Discussion started by: a25khan
3 Replies

Featured Tech Videos