Biswajit Satapathy
Biswajit Satapathy

Reputation: 11

How to know a code in php run sucessfully or not

I have the fallowing code

<html>
<body>
<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
 echo "Hello";
  exec ("chmod a+x ps.sh");

  exec ("sh ps.sh");
}
?>

<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<a href="?run=true">Click Me!</a>

Now i want to know exec ("chmod a+x ps.sh") is executing properly or not. What should i do??

Upvotes: 0

Views: 138

Answers (3)

Kethryweryn
Kethryweryn

Reputation: 639

exec() accepts other parameters. The second one is output, it allows you to see the output of your command.

In the case of a chmod, a correct output is nothing.

The third argument of exec() is the return status. It should be 0 in case of success.

You should then do something like :

exec ("chmod a+x ps.sh", $out, $value);

if(!empty($out) || $value != 0)
{
    #there was an error
}

Note : you don't have to initialize $out or $value beforehand, PHP will create them as you use them.

Upvotes: 0

akluth
akluth

Reputation: 8583

Have a look at the documentation:

string exec ( string $command [, array &$output [, int &$return_var ]] )

...

return_var

If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

So just check if the return code is not equals zero:

exec ("chmod a+x ps.sh", $output, $return);
if ($return != 0) {
    // An error occured, fallback or whatever
    ...
}

Upvotes: 1

deceze
deceze

Reputation: 522626

exec(..., $output, $return);

if ($return != 0) {
    // something went wrong
}

Capture the return code by supplying a variable name for the third parameter. If that variable contains 0 afterwards, all is good. If it's anything other than 0, something went wrong.

Upvotes: 0

Related Questions