shell_exec() returns null or an empty result
var_dump(shell_exec('...')) prints NULLShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
- 1
Running the command in a terminal prints a message, but shell_exec gives nullThe command writes its error to stderrFix - 2
Call to undefined function shell_exec(), or the function is simply absentshell_exec is listed in disable_functionsFix - 3
The 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
- 1
What does the error look like?
var_dump(shell_exec('...')) prints NULLCall 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
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,system3Verify
Run phpinfo() or the CLI check through the web server and confirm the list.
sudo systemctl restart php8.3-fpmConfirmed by php.net
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
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
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack 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 fixBack 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 fixBack end
CodeIgniter 3 stops with "Your system folder path does not appear to be set correctly"
503 Service Unavailable
CodeIgniter 3's front controller resolves $system_path and then checks that the result is a directory.
3 causes, each with a fix