ksh (so probably bash) variables are:
$$ PID of current process
$PPID PID of parent of current process
$! PID of last backgrounded process (as mentioned above).
Main Topics
Browse All TopicsHi
I need to get the PID of a program from a bash script.
The Script will be something like as follows
#!/bin/bash
# I need the pid of process created by the PHP Script that follows.
php --this-option --that-option --another-option --etc
pid={what_do_I_put_in_here
profile_filename=/tmp/apd/
I think this is easy. If not, I've no problem in bumping the points.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I doubt that you can get the PID of a process that was started in the past (and is no longer running). I guess your best bet is to modify your PHP program so that it reports it's own PID (either on stdout, so that you can assign it directly to a variable, or write it to a file with a well known location - even though your application would no longer be able to handle multiple parallel invocations)
Let's assume the PHP program is writing it's PID to stdout:
pid=`php --this-option --that-option --another-option --etc`
I don't think PHP differenciates between stdout, and stderr There will be valid output coming from stdout, that I don't want to mix with the PID reporting.
I like your idea about self reporting, though, maybe I could use an enviornmental variable. Env variables seem to lose their setting once the program has terminated however. The reason I need this is because the Advanced PHP Debugger sends profiling information to /directory/filename.pid. I don't actually want to use different instances of this program at the same time. That is just the way the script works. I'm just trying to make a wrapper so that I can
php_profile whatever.php, and see the output,
instead of
php whatever.php
cd /directory
ls -t1
pprof {newest file}
Hmmm maybe symlinks can solve my problem, if I get PHP to make a symlink(whose name will be constant, and not dependent on the PID), to filename.pid, I can use that symlink instead.
Since that is a variation of getting the script to report its own pid, I'm accepting the suggestion from khkremer as an answer, and the others as helpful(since they may later be incorporated in, if I choose to make the script multi-user)
Business Accounts
Answer for Membership
by: arjanhPosted on 2003-12-07 at 07:14:31ID: 9892019
Hi, the variable $! expands to the process ID of the most recently executed background (asynchronous) command.
So
php --this-option --that-option --another-option --etc
pid=$!