Solved

Getting PID in Bash

Posted on 2003-12-07
8
45,285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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 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
 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pipe to sed or perl, please help 5 145
shell script working with --daemon 18 100
troubleshooting a process that crashes within 3 mins 11 101
Linux Scripting 3 190
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…
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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