Search code examples
phpbashxamppremote-accessremote-server

How to trigger Shell Scripts through PHP


Based on certain form entries from a PHP front end user form, I need to persist the data in a file (already achieved) and after which execute a shell script as an action. I am able to write to a file from PHP however I am not able to trigger another file of the same directory with exactly same permissions for both. I have tried both exec and shell_exec, but doesn't seems to help ! Some of the sample tried includes -

shell_exec("/bin/sh  dq_files/shell_script.sh");
shell_exec("./dq_files/shell_script.sh");
shell_exec("sh dq_files/shell_script.sh");
exec("<similar-shell-functions-as-above>, $result, $output);

All of which didn't help.

Note - My php.ini file's disable_functions is empty (disable_functions=) and my PHP's Safe mode is Off. The PHP is being executed by apache user and apache group, I have updated all files in that directory and till the lowest hierarchy with 777 permissions and ownership has been transferred to apache:apache, but didn't seem to benefit.


Solution

  • This can be solved in multiple ways, as listed below:

    1) Through user-level permissions: Either elevating your current user to allow him to execute at that location by default or through the necessary permission allocation.

    2) Using an independent microservice with the necessary permissions to execute the *.sh files at those locations.

    3) If you have setup run permissions at a different location, you are better off running from that location (if your system administrators won't allow you to add an independent microservice or new set of permissions): php -f /path/to/file.php.

    Hope it helps.