3

(Seemingly) no matter what command I try to run with PHP's system function, the command always does nothing and then fails with exit code 1. For example,

system("echo 'this should definitely work'", $retval);

sets $retval = 1. So does

system("dir", $retval);

as well as running an executable that I've written; when I run

vfmt -h cat.v

from cmd.exe the command works and returns with exit code 0, but running

system("vfmt -h cat.v", $retval);

again sets $retval = 1. This vfmt.exe file is in the same directory as the src.php script that is attempting these system calls.

I am nearly at my wit's end trying to figure out what's wrong. What could possibly be causing this issue?

feralin
  • 3,268
  • 3
  • 21
  • 37
  • 2
    You should incluclude to the question informations about your environment, such: what server, how do you run php (e.g. server-loaded or php-cgi) and such.. And also some lines from the error-log-file ... Othervise, all answer would only "guessing" - such mine bellow. – clt60 Aug 24 '14 at 10:11
  • Add 2>&1 to the end of your command to redirect errors from stderr to stdout. This should make it clear what's going wrong. see http://stackoverflow.com/questions/538939/php-exec-will-not-execute-shell-command-when-executed-via-browser?rq=1 – Dincho Todorov Aug 27 '14 at 17:50
  • 2
    Try to add path to vfmt.exe, for example: `system("c:\\path_to_exe\\vfmt.exe -h cat.v", $retval);` – Rimas Aug 28 '14 at 14:55
  • Are you missing a close quote in the original script, or just the question as posed? – beroe Aug 29 '14 at 22:58
  • @beroe just the question as posed... sorry, I'll fix it. – feralin Aug 30 '14 at 14:13
  • Please give us your `php_info()`. Guessing is soooo boring. You don't have Safe Mode enabled, do you? – Markus Malkusch Aug 30 '14 at 17:41

4 Answers4

2

You should check your php.ini for line like the next:

disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
                   ^^^^          ^^^^^^^^^^^^^^^^^^

and such.

Also check your "safe mode" status, (php ver. < 5.4) if you have enabled it you can only execute files within the safe_mode_exec_dir and so on...

More information in the doc and for the execution of commands here and especially for system here.

clt60
  • 62,119
  • 17
  • 107
  • 194
  • My `php.ini` has `disable_functions=`, and no lines that have anything to do with 'safe mode'. So this is not the issue, unfortunately. – feralin Aug 27 '14 at 01:26
  • There are two php.ini sometimes, one for command-line interface and one for apache. /etc/php/7.4/apache2/php.ini or sudo vi /etc/php/7.4/cli/php.ini. Make sure you are modifying the right one. And of course you need to restart apache. – user1097111 Aug 31 '21 at 21:43
1

echo is invariably a shell internal command, not a separate executable. You need something more like

system('/bin/sh -c "echo \'foo\'"'); // unix-ish
system('cmd.exe /c echo foo'); // windoze
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks for the comment, but using `cmd.exe -c vfmt -h cat.v` still doesn't work... – feralin Aug 18 '14 at 17:17
  • whoops. sorry. it's `/c`. – Marc B Aug 18 '14 at 17:19
  • ... and neither does `cmd.exe /c vfmt -h cat.v`. Any other ideas? – feralin Aug 18 '14 at 17:19
  • vfmt doesn't need the cmd stuff, since it is an executable. try `exec('vfmt ...', $output, $ret); var_dump($output)`. – Marc B Aug 18 '14 at 17:21
  • That's what I thought. So that leads back to my question of "why doesn't `vfmt -h cat.v` work in a PHP `system` call when it does in `cmd.exe`? – feralin Aug 18 '14 at 17:22
  • try the exec() version and look at the output. your system call doesn't return ANY output, so you'd never see any error messages/warnings. – Marc B Aug 18 '14 at 17:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59528/discussion-between-feralin-and-marc-b). – feralin Aug 18 '14 at 17:26
1

Most likely issue is that the "working directory" is not what you expect.

This is hidden as you're checking return value and not screen output; they are different.

Firstly, to monitor: try using exec http://au2.php.net/manual/en/function.exec.php as this will help you debug by giving both screen output and return value.

Secondly, to fix:

1 - Specify full paths to both the executable and included files. Assuming this is Windows (you refer to cmd.exe) then run

system("c:\PathToPhpFile\vfmt -h c:\PathToCatVFile\cat.v, $retval);

or

2 - Change the working directory before calling system() or exec() by using chdir() first.

(Or you can do the cd in a batch file (windows) or concatenate the commands in Linux; but this is less portable.)

Robbie
  • 17,605
  • 4
  • 35
  • 72
1

You need to redirect the output of the command you're running with system() in order for it to run in the background. Otherwise, PHP will wait for program execution to end before continuing.

system('mycommand.exe > output.log', $retval);
system('mycommand.exe 2>&1', $retval);

Not sure how you're invoking your script, but perhaps you're running into a PHP max_execution_time (or related) limit. Something to investigate. Also, since you're running on windows, you may need to use absolute paths with your commands when executing system calls as the file you're invoking may not be in any defined PATH.

Leo Bedrosian
  • 3,659
  • 2
  • 18
  • 22