1

I try run the function exec() to execut another script php. It's work on Windows, but on Linux Server dosen't.

Windows

$str = exec("start /B php test_file.php",$output);

Linux

$str = exec("php test_file.php > /dev/null &",$output);

test_file.php

echo "Test work";

Both scripts are on same folder, when a run on Windows i get a array with the string "Test work", but when i run Linux comand on Linux Server dosen't

EDIT

I try change the comand, as @Robert sugest:

$str = exec("php test_file.php",$output);
echo $str;
var_dump($output);

But that returned this:

array(1147) { [0]=> string(24) "X-Powered-By: PHP/7.0.16" ...
Ruhan O.B
  • 75
  • 1
  • 8
  • 1
    You're redirecting the output to `/dev/null` why is that expected to return the result to PHP? – apokryfos Feb 23 '17 at 13:01
  • I need to return to the variable $output the result of the script to do a validation. How should the command be? – Ruhan O.B Feb 23 '17 at 13:07
  • 1
    Then you should use, `$str = exec("php test_file.php", $output);`. Adding `> /dev/null` will redirect output to.. well `/dev/null`, so `$output` will remain empty. Also you use the `&` at the end, which means that the command will run in the background and (except if you are not redirecting output to somewhere) the exec() command will not wait for the command to finish. – Robert Feb 23 '17 at 13:12
  • @Robert I try u sugestion, but dont work. – Ruhan O.B Feb 23 '17 at 13:25
  • What happens when you execute `php test_file.php` from the command line on your linux machine? – Robert Feb 23 '17 at 13:30
  • @RuhanDeOliveiraBaiense check: http://stackoverflow.com/questions/2443575/how-could-i-stop-php-from-returning-headers-when-executed-from-commandline – Robert Feb 23 '17 at 13:32
  • @Robert I run the comand and i get the correctly response, "Test work". But on comand line i need to put the full path to the file "/home/citronn/public_html/test/test_file.php" – Ruhan O.B Feb 23 '17 at 13:50
  • it's always good practice to use the full path to both the php binary as the file. try finding the cli one using `which php` when on the command line. Then use `exec('/path/to/php -f /path/to/file', $output); – Robert Feb 23 '17 at 14:01
  • @Robert i try as u sugest: exec("usr/local/bin/php -f /home/citronn/public_html/teste/test_file.php",$output); , and it's returned a empty array on $output, and no error's – Ruhan O.B Feb 23 '17 at 14:22
  • Don't forget the / before /usr – Robert Feb 23 '17 at 14:30
  • @Robert Tks , taths worked forme : exec("/usr/local/bin/php -f /home/citronn/public_html/teste/test_file.php",$output); – Ruhan O.B Feb 23 '17 at 14:46
  • @Robert Last question, how i can pass parameters to this file? I try exec("/usr/local/bin/php -f /home/citronn/public_html/teste/test_file.php parameter1",$output); , but dont work – Ruhan O.B Feb 23 '17 at 14:54
  • Let me post a normal answer with everything in it so you can accept it as correct. – Robert Feb 23 '17 at 15:21

1 Answers1

0

Make sure you use the correct path to both your php binary and the file you want to execute.

Example:

exec('/path/to/php -f /path/to/file.php', $output);

Additionally, if you want to pass parameters to it, you can add them after the php file and read them from the $argv array. php manual $argv

In your case you can do:

exec('/path/to/php -f /path/to/file.php param1 param2 param3', $output);

In the php file you are executing, you can do a

vardump($argv);

Which in my example contains 4 items, $argv[0] is the name of the file you are executing, $argv[1] has the value of param1, etc.

Robert
  • 5,703
  • 2
  • 31
  • 32