Perl system() process Id

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HugoFromBOSS
    New Member
    • Sep 2007
    • 38

    #1

    Perl system() process Id

    Hello,

    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!

    Hugh
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Code:
    system (./app -var1 -var2 -var3 > /dev/null 2>&1 &');
    open (PS, 'ps xu|') or die "$!";
    my @processes = <PS>;
    close PS;
    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.

    Comment

    • HugoFromBOSS
      New Member
      • Sep 2007
      • 38

      #3
      Yes, but how do I select the process Id thats unquie to the running application.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Select the id? I don't understand. Select it in what way? Did you run the code I posted? What did it return?

        Comment

        • HugoFromBOSS
          New Member
          • Sep 2007
          • 38

          #5
          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.

          Thanks

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            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.

            Regards,

            Jeff

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              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.

              Comment

              • HugoFromBOSS
                New Member
                • Sep 2007
                • 38

                #8
                CPAN? right I'll take a look cheers!

                Comment

                Working...