Why shell_exec Often Fails and How to Fix It
Using shell_exec() in PHP to run scripts located on the same server is a common pattern, but it frequently encounters permission or environment issues that cause silent failures. In your specific case, the code echo shell_exec('/home/scripts/fix-perm.sh'); returns nothing because the command is likely failing before any output is generated.
When you invoke a shell script directly from PHP, you are not simply calling a binary; you are invoking an interpreter (usually /bin/bash) to execute that script. The most common reason for silence is that the web server process does not have permission to read or execute the file, or the environment variables required by the script are missing.
Checking File Permissions and Ownership
The path /home/scripts/fix-perm.sh suggests a specific ownership structure. Files inside /home directories are typically owned by individual users, while the web server (running as www-data, apache, or nobody) runs under a system user that rarely has write access to user home directories.
If the script is located at /home/scripts/fix-perm.sh, verify who owns it and what permissions are set. You can check this via SSH:
ls -la /home/scripts/fix-perm.sh
You will likely see something like -rwxr-xr-x owned by a specific user. If the web server user cannot execute this file, shell_exec() will return an empty string. To allow execution without changing ownership (which is generally safer), you can add execute permissions for others:
chmod o+x /home/scripts/fix-perm.sh
However, if the script needs to read or write files within /home, the web server user must explicitly have that permission. Often, the cleanest approach is to move scripts to a directory accessible by the web server, such as /var/www/html/scripts or /usr/local/bin.
Handling Environment Variables and Paths
Even if permissions are correct, the script might fail because it relies on environment variables set for your interactive shell session but not present in the web server context. When you run fix-perm.sh manually in SSH, your user profile (e.g., .bashrc) sources necessary paths. PHP, running as a daemon, does not source these files automatically.
If your script contains commands like my_custom_tool or expects $HOME to point to /home/username, the web server's environment will differ. A robust way to handle this is to pass arguments directly to the shell or use passthru() with error output redirection.
Modify your PHP code to capture errors:
$output = shell_exec('/home/scripts/fix-perm.sh 2>&1');
echo "<pre>";
echo htmlspecialchars($output);
echo "</pre>";
?>
Redirecting stderr (2>&1) is crucial. By default, shell_exec() suppresses error messages in the output stream, making debugging nearly impossible if the script fails silently. Seeing the actual shell error message will tell you immediately if it's a "command not found" or "permission denied" issue.
Using Absolute Paths for Executables
Another frequent pitfall is assuming that commands inside the shell script are found in the system PATH relative to the web server user. If fix-perm.sh calls a utility installed locally, ensure that utility is either absolute (e.g., /usr/local/bin/tool) or that the PATH environment variable includes its directory. You can debug this by adding echo $PATH inside the script itself during manual testing.
When to Use exec() Instead of shell_exec()
If you need more control over the process, consider using exec(). Unlike shell_exec(), which returns a single line of output, exec() allows you to capture the return status of the command and stream output if needed. However, for simple script execution where you just want the result or error log, shell_exec() with stderr redirection is usually sufficient.
If your application framework is Laravel, managing shell commands can be handled more elegantly using the Process facade, which provides better control over working directories and environment injection. For instance:
use Illuminate\Support\Facades\Process;
$process = Process::run('/home/scripts/fix-perm.sh');
echo $process->output();
echo $process->errorOutput();
This approach is often preferred in modern PHP ecosystems because it abstracts away some of the shell quirks and makes error handling more explicit. Regardless of the method chosen, always ensure the script is executable and that the executing user has the necessary filesystem permissions to perform any operations defined within the script.