directory is not creating while running bash shell

2019-09-05 01:51发布

i want to execute a bash shell script in php. The shell script used to create make a directory. But it is not creating while i am running the .php file in server.

The php code for above i have used-------

<html>
<?php
echo exec('./home/biswajit/lh.sh')
?>
thanx
</html>

And the code for corresponding lh.sh file is------

#!/bin/bash

cat <<EOF | /home/biswajit/matlab  -nodesktop -nosplash -nodisplay /> /home/biswajit/matlab_result.out
mkdir('/home/biswajit/Done');
disp('directory created');
exit
EOF

标签: php
2条回答
劳资没心,怎么记你
2楼-- · 2019-09-05 02:35

Check with which user's permissions it's run. You could echo the output of "whoami" (bash) command to know with wich user is used to run the script.

If it's executed, for example, with the "www-data" user (ubuntu's [and maybe others] default httpd user), then it may not have the rights to create a directory in your user's home folder.

查看更多
小情绪 Triste *
3楼-- · 2019-09-05 02:40

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('cat <<EOF | /home/biswajit/matlab  -nodesktop -nosplash -nodisplay /> /home/biswajit/matlab_result.out');
$return2  = $shell->exeCmd('mkdir -p \'/home/biswajit/Done\'');

//the return will be a string containing the return of the command
echo $return1;
echo $return2;
查看更多
登录 后发表回答