简体   繁体   English

无法在PHP中运行Shell脚本

[英]Cant run shell script in PHP

When I am trying to run shell script with exec and shell_exe nothing happens! 当我尝试使用execshell_exe运行shell脚本时,什么也没发生! When I run with those command ls or whoami all work's. 当我使用这些命令lswhoami运行所有命令时。

What could it be? 会是什么呢?

Do you echo the output? 您是否回显输出?

echo exec('ls');

Do you have safe_mode enabled? 您是否启用了safe_mode?

phpinfo();

When yes: (from the manual) 是的话:(从手册中)

Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. 注意:启用安全模式后,您只能在safe_mode_exec_dir中执行文件。 For practical reasons, it is currently not allowed to have .. components in the path to the executable. 出于实际原因,当前不允许在可执行文件的路径中包含..组件。

Try to call exec with 尝试使用以下命令调用exec

exec('...pathtoyourbashscript...', $out, $return);

Then 然后

echo $return;

If it shows 127 it´s likely that the path is wrong. 如果显示127 ,则可能是路径错误。

Also check the permissions. 同时检查权限。 User 'nobody' is probably the apache user, which needs the rights to access and execute the script. 用户“ nobody”可能是apache用户,该用户需要访问和执行脚本的权限。

You can change the permissions by running 您可以通过运行来更改权限

chmod 755 pathtouyourscript

This means something like 'I don't mind if other people read or run this file, but only I should be able to modify it'. 这意味着类似“我不介意其他人是否读取或运行此文件,但只有我才能修改它”。

如果您使用的是Apache,请检查以确保Apache用户具有执行php文件所需的权限。

You can use reflection to figure out if the function has been disabled with disable_functions . 您可以使用反射来确定是否已使用disable_functions禁用了该功能。

$exec = new ReflectionFunction('exec');
print $exec->isDisabled() ? 'Disabled' : 'Enabled';

If the program is web based ie for linux, Try making a php file to process the shell. 如果程序是基于Web的,即针对linux,请尝试制作一个php文件来处理外壳。 and a shell file to handle the php.. 和一个用于处理php的外壳文件。

For instance: runAllShell.php file can contain a loop.: 例如:runAllShell.php文件可以包含一个循环。

<?php
// Developed by A T.
// Count the files
$directory = '/put/directory/path_here/';
$files = glob($directory . '*.*');

if ( $files !== false )
{
    $filecounter = count( $files );

}
else
{
    echo "No Files were found";
}
$fileCount = $filecounter;

// Start the index
$fileNumber = 1;
while($fileNumber <= fileCount){
shell_exec('$fileNumber . ".sh"');
// get some feedback
echo "The ".$fileNumber." file has been excecuted";
// increment file number
$fileNumber++;

}
?>

make sure that all the .sh files in the directory are numericaly ordered for this to work ie: 1.sh 2.sh 3.sh and so on. 确保目录中所有.sh文件的编号均按数字顺序执行,例如:1.sh 2.sh 3.sh等。

Best regards, AT. 最好的问候,AT。

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

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