Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

shell_exec() returns null or an empty result

Errorvar_dump(shell_exec('...')) prints NULL

Short answer

shell_exec returns null when the command failed or printed nothing to standard output, and it cannot tell you which. Redirect stderr into stdout to see the real error, use exec() to read the exit code, and check that shell_exec is not listed in disable_functions in the php.ini that PHP uses.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the same command with stderr merged into stdout and with the exit code captured. This separates a failing command from a blocked function.

    <?php
    exec('your-command 2>&1', $out, $code);
    var_dump($code, $out);
  2. 2Match what you see, then jump to its fix

    1. 1Running the command in a terminal prints a message, but shell_exec gives nullThe command writes its error to stderrFix
    2. 2Call to undefined function shell_exec(), or the function is simply absentshell_exec is listed in disable_functionsFix
    3. 3The command works in your shell but not from the web page; with 2>&1 you see 'not found'The web user's PATH does not contain the programFix

What does the error look like?

  • var_dump(shell_exec('...')) prints NULL
  • Call to undefined function shell_exec()

Cause 1 of 3

The command writes its error to stderr

1Check

shell_exec returns only standard output. If the command prints nothing there, the result is null even when it failed.

php -r 'var_dump(shell_exec("ls /missing"));'

2Fix

Append 2>&1 so error text arrives in the return value, or use exec() when you need the exit code.

$out = shell_exec('your-command 2>&1');
echo $out;

3Verify

The message from the command is now visible.

php -r 'echo shell_exec("ls /missing 2>&1");'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

shell_exec is listed in disable_functions

1Check

disable_functions is read from php.ini only (never from httpd.conf), and the web server often uses a different php.ini than the command line. Since PHP 8.0 a disabled function is removed entirely.

php --ini
php -r 'echo ini_get("disable_functions");'

2Fix

Remove shell_exec from disable_functions in the php.ini that the web server's PHP loads, then restart PHP-FPM or Apache. On shared hosting, ask the host. Keep it disabled if you cannot trust the code.

; php.ini
disable_functions = exec,passthru,system

3Verify

Run phpinfo() or the CLI check through the web server and confirm the list.

sudo systemctl restart php8.3-fpm

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The web user's PATH does not contain the program

1Check

The PHP process runs with its own, often short, environment, so a bare program name may not resolve.

which your-command
php -r 'echo shell_exec("echo $PATH");'

2Fix

Call the program by its full path.

$out = shell_exec('/usr/local/bin/your-command --version 2>&1');

3Verify

The output now shows the version.

php -r 'echo shell_exec("/usr/bin/env 2>&1");'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Prefer exec() or proc_open() when you need exit codes and stderr separately.
  • Escape user input with escapeshellarg() before it reaches a command.
  • Use full paths to programs in scripts that run from the web.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.