Sunday, March 20, 2011

taskkill from PHP exec

Hello all,

I have just tried to execute this:

function kill_hr(){

    exec("taskkill /IM uper.exe", $output = array(), $return);

    print_r($output);

    echo "<br />".$return;

}

However, the output is this and its not very useful:

Array ( ) 1

When the process doesn't exist its this:

Array ( ) 128

I am trying to work out why it gives me a 1 when the process exists - is this a permissions problem? If so, how can I rectify this?

From stackoverflow
  • The $return value is the value the program returns. In Windows, the return values 0 and 1 is usually used to indicate success, so you can easily tell when the termination was successfull.

    However, a return value of 128 is arbitrary, meaning that the program developers of taskkill decided on it themselves. 128 likely means that the process doesn't exist.

    There does not seem to be any documentation that documents the return values of taskkill, unfortunately.

    If your goal is to prevent uper.exe from being present, a return value of 128 and 1 would then both be acceptable, and your code becomes:

    function kill_hr()
    {
        exec("taskkill /IM uper.exe", $output = array(), $return);
        return $return == 1 || $return == 128;
    }
    

    The function will return true if uper.exe was successfully terminated, or if it wasn't running in the first place.

    Edit: Re-reading your post, you can try the following; use runas to start a command prompt as your web server user (from an administrative command prompt):

    runas /user:account@machine cmd
    

    Then you will have a command prompt running as your web server, and you can issue the taskkill command from there. Then you will likely see a textual error message.

    Abs : I don't think I explained my problem well! :) The problem is the taskkill isn't being successful because it doesn't return 0 and I can see in the task list that the uper.exe isn't being killed - my question is why isn't it being killed and how can I do this?
    Vegard Larsen : Try starting a command prompt as the web server user (find out which user Apache or IIS runs as), and run the `taskkill` command from there. That should probably give you an error message on the command line.
  • I've had this problem before - a few things what worked in the past:

    Solution #1

    Launch taskkill with the Windows startcommand:

    exec('start /B taskkill /IM notepad.exe', $output = array(), $return);

    Solution #2

    Open a new command-line using cmd.exe:

    exec('cmd /c taskkill /IM notepad.exe', $output = array(), $return);

    *Note: I've used both methods in the past to launch background processes from PHP - I'm not sure what the return values will be, so you'll need to experiment.

    Abs : Thanks for your reply and useful suggestions: I tried both and the cmd solution gave me the same as what I had before. But for the start it returned a success - got excited but I saw that it didn't kill the process but still returned success - Whats going on? how can I get it to kill!!
    Vegard Larsen : When you use `start`, what happens is that you get back the return value from the `start` command, not from `taskkill`. `start` succeeds, but you have no idea if `taskkill` succeeds.
  • In addition to the other answers, the numeric value returned by the command is called the ERRORLEVEL in Windows. When playing around with taskkill on the command line, you can make the last errorlevel returned visible using

    echo %ERRORLEVEL%
    
  • Thank you all for your help - I finally managed to kill the process using this:

    $output = shell_exec('taskkill /F /IM "uper.exe"');
    

    I am sure it will work with exec as well, but the important part is the /F forcing it! :)

0 comments:

Post a Comment