Solved

Getting PID in Bash

Posted on 2003-12-07
8
45,246 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
qos on linux 3 96
shell script help 1 141
RHEL 7 , I have tftp running as a process how can I run it as a service 2 77
Help with Ubuntu 14.04 LTS 10 132
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 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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

19 Experts available now in Live!

Get 1:1 Help Now