php shell_exec() command is not working

php shell_exec() command is not working

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why shell_exec() Returns Empty Output in PHP

When a script runs successfully from the command line but fails inside a web server environment, the immediate assumption is often that the code is broken. In your case, the discrepancy between running php /var/www/project/web.php and loading it via a browser provides the critical clue: the execution context has changed.

The output showing only "done" confirms that your PHP script executed without crashing, but shell_exec() returned an empty string. This usually means the shell command failed silently or was never invoked correctly by the web server process. The fact that you set permissions to 777 on both .sh and .py files suggests a misunderstanding of how file permissions interact with user contexts in production environments.

The User Context Difference

The most common reason for this behavior is that the www-data user (or whatever user your web server runs as) does not have permission to execute the script or access required resources, even if you set them to 777 from your own terminal.

When you run commands from your terminal, you are likely logged in as your personal user (e.g., root, vagrant, or a standard system user). If you own these files and set permissions to 777, they work for you. However, when the web server processes a request, it drops privileges to run under a specific service account.

To verify this, check the ownership of your files:

ls -l /var/www/project/xxe.sh /var/www/project/vin.py

If the owner is not root or www-data, and you haven't explicitly changed group permissions, the web server might be blocked. While 777 technically grants everyone execute permission, it can also trigger security modules like SELinux or AppArmor to block execution if the process type isn't expected. A more secure and standard approach is to make the files executable for all users while keeping ownership consistent:

chmod +x /var/www/project/xxe.sh
chown www-data:www-data /var/www/project/xxe.sh /var/www/project/vin.py

Environment Variables and Paths

Another frequent culprit is the environment. The shell script xxe.sh calls python vin.py. When running from your terminal, Python uses your system's PATH and your local environment variables (like PYTHONPATH, VIRTUAL_ENV, or specific library paths).

The web server runs in a restricted environment. It often lacks access to virtual environments you created in your home directory or does not have the necessary libraries installed globally. If vin.py relies on a specific Python package that isn't installed for the system Python, the script will crash immediately after starting, returning no output.

You can debug this by adding explicit error reporting to your shell script before calling Python:

#!/bin/bash
set -x # Enable verbose mode to see exactly what command fails
python /var/www/project/vin.py 2>&1 | tee /tmp/script_output.log
echo "Script finished"

Run this from the terminal first. If you see an error in /tmp/script_output.log when running via shell_exec(), that is your diagnosis. The error might be a ModuleNotFoundError or a path issue where python cannot find the interpreter.

Security and Web Server Configuration

Even if permissions and paths are correct, web servers often strip environment variables for security reasons to prevent arbitrary command execution. Functions like shell_exec(), exec(), and passthru() are inherently dangerous because they can lead to Remote Code Execution (RCE) vulnerabilities if user input is passed directly into the command string without sanitization.

Modern PHP installations also have the open_basedir restriction enabled by default in production. This limits the directories a script can access. If your shell script tries to read files outside the allowed directory, it will fail silently. You can check this setting in your php.ini or .user.ini:

open_basedir = /var/www/:/tmp/

Additionally, some hosting providers or security configurations disable these functions entirely via the disable_functions directive. If you need to execute system commands, consider using Process Managers like Supervisor for long-running tasks rather than relying on web hooks, or switch to a more controlled mechanism like a queue job in Laravel (if applicable) that handles background execution safely.

Debugging Output Visibility

Finally, remember that shell_exec() returns the output as a string. If the script generates massive output, it might be truncated depending on PHP's output_buffering settings or memory limits, though usually, it just returns an empty string if the underlying process exits with a non-zero status code. Always capture the exit code to confirm success:

$output = shell_exec('/var/www/project/xxe.sh');
$exitCode = shell_exec('echo $?'); // Note: This requires capturing stderr correctly or using proc_open for better control

For robust execution, proc_open provides access to both stdout and stderr separately, making it much easier to identify exactly where a pipeline breaks.

Our network