Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Getting process ID number: PHP

Options
  • 09-07-2009 9:37pm
    #1
    Closed Accounts Posts: 1,444 ✭✭✭


    How do I get the process id number into my PHP code.

    I'm running a 'tail -f' command to monitor a file. How do I kill the tail process once I'm finished with it? I've just discovered a whole load of tails running when I run 'pgrep tail'!

    [PHP]
    $command="tail -f ".$file_being_monitored." 2>&1";
    $handle=popen($command, 'r');

    while(!feof($handle))
    {
    //monitor the file, take actions etc.
    [/PHP]

    Any help would be much appreciated.

    Thanks in advance.


Comments

  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    Just close the file handle when you are finished with it:

    http://us3.php.net/manual/en/function.pclose.php


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Just close the file handle when you are finished with it:

    http://us3.php.net/manual/en/function.pclose.php

    Yes, but is there a way of doing this externally from a command line and not inside the PHP program?

    I can't echo the $handle variable as it's an object and not a process id number or something I can easily kill.

    Any suggestions?


  • Moderators, Education Moderators, Home & Garden Moderators Posts: 8,120 Mod ✭✭✭✭Jonathan


    Could you echo ps at the end of your script and parse the output?

    Kill any processes based on their pid that way?


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    I tried killing the PHP process using the getmypid() function. But the tail still remains open. Any way of getting the pid on calling the tail?

    Is there a way of getting the PID number from the file handle?


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Jonathan wrote: »
    Could you echo ps at the end of your script and parse the output?

    Kill any processes based on their pid that way?

    Thought about doing something like that...

    But there are multiple clients accessing this php file at once. How do I know what PID to kill?


  • Advertisement
  • Moderators, Education Moderators, Home & Garden Moderators Posts: 8,120 Mod ✭✭✭✭Jonathan


    Cantab. wrote: »
    Thought about doing something like that...
    Never mentioned that... :P
    Cantab. wrote: »
    But there are multiple clients accessing this php file at once. How do I know what PID to kill?
    I *think* unix PID's are sequential. Kill the lowest value PID?


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Jonathan wrote: »
    Never mentioned that... :P

    I *think* unix PID's are sequential. Kill the lowest value PID?

    Mmm. That might work.

    I'm thinking of re-writing the whole thing in perl.

    For example, this might work:
    my $pid = open(FH, "tail -f $filename" ) or warn "$!\n";
    


  • Moderators, Education Moderators, Home & Garden Moderators Posts: 8,120 Mod ✭✭✭✭Jonathan


    Cantab. wrote: »
    I'm thinking of re-writing the whole thing in perl.
    What does the whole thing do any way?


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Jonathan wrote: »
    What does the whole thing do any way?

    Instant messaging over port 80. C# windows mobile app communicating with PHP/MySQL server.

    It works fine, it's just there's a load of tail processes hanging around that are causing problems.


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    I don't get it - you want to kill loads of tail processes from the command line?

    kill -9 `ps ax | grep 'tail -f'`

    or

    killall tail


  • Advertisement
  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    I don't get it - you want to kill loads of tail processes from the command line?

    kill -9 `ps ax | grep 'tail -f'`

    or

    killall tail

    I don't want to kill them all, I want to kill a specific one.


  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    Sorry, I'm really confused here - long week - why are you starting a tail inside a program that you want to stop by killing it outside the program? I don't think my advice is any good because I don't get the issue... :confused:

    If you want to manually kill the process, you might as well just run ps and manually find the process you want to kill? Use the forest output of ps ('f' I think - man ps) if you need to see what tails belong to what php processes...


Advertisement