简体   繁体   中英

taskkill from PHP exec

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?

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! :)

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%

I've had this problem before - a few things what worked in the past:

Solution #1

Launch taskkill with the Windows start command:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM