简体   繁体   English

如何从php / html执行shell脚本?

[英]How do I execute a shell script from php/html?

How to execute shell script files from PHP or HTML. 如何从PHP或HTML执行Shell脚本文件。 The sh file is located outside the localhost directory, and there are many other sh files which are initiated by the first sh file. sh文件位于localhost目录外部,并且还有许多其他的sh文件由第一个sh文件启动。

say the local host directory is /opt/lampp/htdocs/xampp/test and the location of the sh file is /home/user/scripts/run.sh . 例如,本地主机目录为/opt/lampp/htdocs/xampp/test ,sh文件的位置为/home/user/scripts/run.sh

run.sh copies some files uploaded from /opt/lampp/htdocs/xampp/test/upload to /home/user/scripts/file and execute some other .sh files. run.sh将从/opt/lampp/htdocs/xampp/test/upload一些文件复制到/home/user/scripts/file并执行其他一些.sh文件。

I have tried many PHP methods with no success like 我尝试了许多PHP方法,但都没有成功

<?php $output = shell_exec('sh /home/user/script/run.sh'); echo "$output"; ?>

and

<?php
shell_exec("/home/user/server/run.sh");
?>

but no luck. 但没有运气。 I could not put the sh file in CGI directory as it is dependent on other script and files. 我不能将sh文件放在CGI目录中,因为它依赖于其他脚本和文件。 How do I run the script from the directory it currently presents. 如何从当前显示的目录运行脚本。

It's most likely the case that you do not have any environment variables setup as the user who is executing the script from php. 最有可能的情况是,您没有任何环境变量设置为从php执行脚本的用户。 It's probably username group apache:apache. 可能是用户名组apache:apache。 You should setup some environment variables for PATH so it knows where the executables are. 您应该为PATH设置一些环境变量,以便它知道可执行文件在哪里。 You could alternatively point sh to /usr/bin/sh : 您也可以将sh指向/usr/bin/sh

<?php $output = shell_exec('/usr/bin/sh /home/user/script/run.sh'); echo "$output"; ?>

Or log onto the server and echo $PATH . 或登录到服务器并echo $PATH Then copy paste that into the putenv function: 然后将其复制粘贴到putenv函数中:

putenv('PATH={your_paths}');
$output = shell_exec('sh /home/user/script/run.sh'); echo "$output";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM