Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting PID in Bash

Posted on 2003-12-07
8
Medium Priority
?
45,328 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 200 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 400 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
 
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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

773 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