Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting PID in Bash

Posted on 2003-12-07
8
Medium Priority
?
45,312 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 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 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