Running .sh scripts via PHP

I have several game servers that I need to run shell scripts for frequency. I am trying to figure out how to run these scripts using a webpage on the same server. This is a dedicated Ubuntu server.

Website files are located through /var/www/... The .sh files that I need to run manually are located in /home/amservers/.../start.sh .

I looked at other answers and I still can not understand. How to find files and save them and then run exec ()?

+4
source share
3 answers

You can simply use the shell_exec () function in PHP:

http://php.net/manual/en/function.shell-exec.php

 shell_exec('sh script.sh'); 

And if you want to use variables ($ 1, $ 2, etc. in bash), you can simply enter:

 shell_exec('sh script.sh variable1 variable2'); 
+9
source

You can use either an absolute path or a relative path. For relative paths, the current directory is usually PHP files. If you cannot access your scripts even with an absolute path, there may be a problem with access rights to the file system. Keep in mind that PHP scripts are usually executed as a web server (system) user account.

Your shell scripts should be stored somewhere close to or inside your application tree so that you can give them access to your PHP scripts (i.e. your web server), without compromising your file system security.

0
source

if you want to check script output in browser than just use

var_dump (shell_exec ('sh script.sh'));

or

var_dump (shell_exec ('sh script.sh variable1 variable2'));

0
source

Source: https://habr.com/ru/post/1488977/


All Articles