Oleg
Oleg

Reputation: 381

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?

Upvotes: 0

Views: 4608

Answers (4)

StarShows Studios
StarShows Studios

Reputation: 99

If the program is web based i.e. 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 i.e: 1.sh 2.sh 3.sh and so on.

Best regards, AT.

Upvotes: 0

Sebastian Ke&#223;ler
Sebastian Ke&#223;ler

Reputation: 1053

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

Upvotes: 2

Wil Moore III
Wil Moore III

Reputation: 7194

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';

Upvotes: 0

Dale Smith
Dale Smith

Reputation: 305

If you're using Apache, check to make sure that the Apache user has the required permissions to execute the php file.

Upvotes: 0

Related Questions