What Are the Differences of System(), Exec() and Shell_Exec() in PHP

What are the differences of system(), exec() and shell_exec() in PHP?

exec — Execute an external program

system — Execute an external program and display the output

shell_exec — Execute command via shell and return the complete output as a string

so if you don't need the output, I would go with exec.

Further details:

  • http://php.net/manual/en/function.exec.php
  • http://php.net/manual/en/function.system.php
  • http://php.net/shell_exec

What is different between exec(), shell_exec, system() and passthru() functions in PHP?

  • exec only returns the last line of the generated output.
  • shell_exec returns the full output of the command, when the command finished running.
  • system immediately shows all output, and is used to show text.
  • passthru also returns output immediately, but is used for binary data. passthru displays raw data.

With both exec and shell_exec it is possible to handle the output yourself, while system and passthru won't let you customize it and immediately display the output.

A more detailed comparison can be found here.

PHP shell_exec() vs exec()

shell_exec returns all of the output stream as a string. exec returns the last line of the output by default, but can provide all output as an array specifed as the second parameter.

See

  • http://php.net/manual/en/function.shell-exec.php
  • http://php.net/manual/en/function.exec.php

Difference between system and shell_exec

With system is possible to capture the return code. Already with the shell_exec is not possible.

PHP system(), exec(), and shell_exec() not returning output

I think that the issue is not that you not get the output of the executed command, but which fails to find mysql.
Using exec you can get the return status of your command, where 0 means successful, and other values indicate an error.

$output = exec($cmd, $output, $retval);
var_dump($output);
var_dump($retval);

If the $retval is 1 that would mean which doesn't find the mysql binary, and returns an empty line.

PHP shell_exec(), exec(), and system() returning only partial output

The siege program writes its output to two different standard streams: stdout and stderr. PHP's exec() only captures stdout. To capture both, you need to redirect (using your shell) stderr to stdout so that everything is in the one stream that PHP captures.

To do this, add 2>&1 at the very end of your command. In your example, that would be:

exec('/usr/local/bin/siege -c30 -t30s -f urls.txt 2>&1', $output);

(I have installed siege and verified that it uses both stdout and stderr and that the output redirection works.)

How should I invoke shell_exec or exec in php if I want all the commands to be run on same shell instance and not on different instances everytime?

  1. Open a new screen session. Name it test:

    screen -S test

  2. Detach from the screen session with Ctrl+A followed by a d.

  3. Send the command to that screen session:

    screen -S test -X stuff 'echo enter command here'`echo -ne '\015'`

  4. Wrap the above command in exec and set up the appropriate permissions (i.e., PHP should be able to access the test screen session).



Related Topics



Leave a reply



Submit