0

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??

  • http://php.net/manual/pt_BR/function.exec.php –  Aug 07 '13 at 13:53
  • this will probably be helpful: http://stackoverflow.com/questions/11875820/checking-exec-runs-successfully-or-not – mavili Aug 07 '13 at 13:54
  • possible duplicate of [directory is not creating while running bash shell script in php](http://stackoverflow.com/questions/18102290/directory-is-not-creating-while-running-bash-shell-script-in-php) – Elias Van Ootegem Aug 07 '13 at 14:07
  • 1
    You've asked the same question before. If you didn't get the answer as fast as you'd have liked it, be patient, keep looking. Don't repost the question until you get an answer that is copy-pastable. This is not a code generator – Elias Van Ootegem Aug 07 '13 at 14:08
  • The `exec ("sh ps.sh")`; is executing a shell script. The is shell script will create a txt file as an output. But actually this is not happening. how to solve this problem. Please give some suggestions. – U-571 Aug 07 '13 at 16:29

3 Answers3

1

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
    ...
}
akluth
  • 8,393
  • 5
  • 38
  • 42
0
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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • The `exec ("sh ps.sh")`; is executing a shell script. The is shell script will create a txt file as an output. But actually this is not happening. how to solve this problem. Please give some suggestions. – U-571 Aug 07 '13 at 16:29
  • 1
    Sorry, my crystal ball is currently unavailable. – deceze Aug 07 '13 at 16:31
  • Thank You, But how i will be able to execute the shell script now?? It is not executing. – Biswajit Satapathy Aug 11 '13 at 11:38
  • @Bis I have no idea *why* the script isn't working and I have no way to debug it for you. Capture all the output and look at all the logs you may have to debug it. – deceze Aug 11 '13 at 11:42
0

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.

Kethryweryn
  • 639
  • 4
  • 11
  • How does `$out` _not_ being empty point to an error? if I called `exec('ls -lta', $out, $value);` then `$out` being empty would certainly be wrong. Besides, `exec` expects _a reference_ to an array as a second argument. Yes PHP will create an array for you, but it's always better to initialize any variables you need yourself. Not sure if that's the case here, but more often than not, PHP issues a warning if you don't – Elias Van Ootegem Aug 07 '13 at 14:00
  • "In the case of a chmod, a correct output is nothing", as said in my answer. He was asking about this precise line. For the $out, php doesn't add values in an existing array, it will replace everything this variable was. You could already have an object, a string or something else in $out, it will be replaced. There is no warning in this case. – Kethryweryn Aug 07 '13 at 14:07
  • OP's snippet shows 2 exec calls, I think it best not to use a vague check like `empty($out)`. I know out will be reassigned, that's why it's passed by reference. You say there is no warning issued, but have you got your ini-settings right (`E_STRICT | E_ALL`)? – Elias Van Ootegem Aug 07 '13 at 14:10
  • Yes, the ini settings are correct, just verified them with a trigger_error(). Just test a sample code if you're having doubts. For the second exec call, you're absolutely right, he should test the $out against what his script is supposed to do. – Kethryweryn Aug 07 '13 at 14:22
  • If you want to know what that script is supposed to do, check the question I marked as duplicate (auto-comment below question). The OP asked this question before. He doesn't seem to know the environment variables might be different, the user running the script might not have the path to `chmod`, or the rights to execute it... adding all that, and a basic tut on `visudo` is a bit much for SO – Elias Van Ootegem Aug 07 '13 at 14:25
  • The `exec ("sh ps.sh")`; is executing a shell script. The is shell script will create a txt file as an output. But actually this is not happening. how to solve this problem. Please give some suggestions. – U-571 Aug 07 '13 at 16:29
  • The exec ("sh ps.sh"); is executing a shell script. The is shell script will create a txt file as an output. But actually this is not happening. how to solve this problem. Please give some suggestions. – Biswajit Satapathy Aug 08 '13 at 04:52
  • Here the value of $value is not zero. Now what is the wrong with the program?? What should I do. Please give me some suggestions. – Biswajit Satapathy Aug 08 '13 at 05:09
  • This means there's a problem with your sh script. Try to run it manually and, if it runs, verify that the user running the php script (probably www-data) has the rights to do what your script is supposed to do. If this still doesn't help you, create a question with said script explaining your problem precisely. – Kethryweryn Aug 08 '13 at 13:21