简体   繁体   中英

Cant run shell script in PHP

When I am trying to run shell script with exec and shell_exe nothing happens! When I run with those command ls or whoami all work's.

What could it be?

Do you echo the output?

echo exec('ls');

Do you have safe_mode enabled?

phpinfo();

When yes: (from the manual)

Note: When safe mode is enabled, you can only execute files within the 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('...pathtoyourbashscript...', $out, $return);

Then

echo $return;

If it shows 127 it´s likely that the path is wrong.

Also check the permissions. User 'nobody' is probably the apache user, which needs the rights to access and execute the script.

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 .

$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. and a shell file to handle the php..

For instance: runAllShell.php file can contain a loop.:

<?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.

Best regards, AT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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