Solved

Getting PID in Bash

Posted on 2003-12-07
8
45,238 Views
Last Modified: 2011-08-18
Hi

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/whatever.$pid

I think this is easy. If not, I've no problem in bumping the points.
0
Comment
Question by:aolXFT
8 Comments
 
LVL 5

Assisted Solution

by:arjanh
arjanh earned 50 total points
ID: 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=$!
0
 
LVL 7

Assisted Solution

by:glassd
glassd earned 50 total points
ID: 9892028
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).
0
 
LVL 6

Author Comment

by:aolXFT
ID: 9892068
Hmmm, the $! variable works if I put an & on at the end to send it to the background.

I don't want to send it to the background though. I just want it to do it's job finish up, and then get it's pid.

I don't want to do anything with the pid either until the php process is finished.
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 100 total points
ID: 9892320
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`


0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Author Comment

by:aolXFT
ID: 9897544
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)
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9900150
aolXFG,
unfortunately it's not sufficient if you state in a comment that you accept a certain answer :-)

PHP has a symlink command that creates symlinks, so you should be in good shape.
0
 
LVL 6

Author Comment

by:aolXFT
ID: 9906564
Sorry about that, I was sure that I'd done the Point Splitting, I specificly remember pressing the button and filling in the 50,50,100 point values. I must have forgotten to press the submit button, :(

Ah well better late than never :)
0
 
LVL 6

Author Comment

by:aolXFT
ID: 9906588
I know now what must have happened, because it happened again.

i filled in the point values, and pressed the submit button, but forgot to select an accepted answer, it then returned me to the page, and instead of reading it again, I simply used the browser window for to get another page.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now