469,271 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Perl system() process Id


I have a the following command executing via the "system" command

system (./app -var1 -var2 -var3 > /dev/null 2>&1 &');

With this application running how would I go about getting its process Id after it's started.

Many Thanks in advance!

Sep 5 '07 #1
7 12603
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. system (./app -var1 -var2 -var3 > /dev/null 2>&1 &');
  2. open (PS, 'ps xu|') or die "$!";
  3. my @processes = <PS>;
  4. close PS;
  5. print "$_" for @processes;
You may need to change 'xu' to whatever is appropriate but you should see the app listed in the "ps" output and get its pid from there.
Sep 5 '07 #2
Yes, but how do I select the process Id thats unquie to the running application.
Sep 6 '07 #3
4,059 Expert 2GB
Select the id? I don't understand. Select it in what way? Did you run the code I posted? What did it return?
Sep 6 '07 #4
Yes, I ran the code.

It returned ALL of my current processes. I just want the one the process Id thats being used by my app. So that I can put it into a string.

$appId = ;

So that I can add it to my database to KILL the application at a later date.

Sep 6 '07 #5
3,503 Expert Mod 2GB
True, what Kevin provided you would provide you with a list of all running processes on the machine. Now, what you will have to do is write some code that will:

1. Look for the entry that has the name of the application you are running.
2. If it finds it, then you will need to parse out of that entry the process ID and save it into a variable.

Why don't you try writing some code to do that and if you get stuck, post what you have here and we can help you troubleshoot it.

We would rather have you try to write it, that way you learn what needs to be done next time.


Sep 6 '07 #6
4,059 Expert 2GB
But what if the process is being run more than once? The output will have multiple entries for the same process with different pid's, you wouldn't know which one to kill unles you wanted to kill them all. You would have to get the pid back right when the applicaion starts to know which one you wanted to kill. One of the PID modules on CPAN might be more appropriate to use for such a specific task as knowing wich pid to kill based on the app name.
Sep 6 '07 #7
CPAN? right I'll take a look cheers!
Sep 6 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Greg Merideth | last post: by
1 post views Thread by Prashant | last post: by
3 posts views Thread by azsx | last post: by
4 posts views Thread by gheim | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.