Executing Bash script from PHP but nothing happens [duplicate] - php

This question already has answers here:
How can I debug exec() problems?
(4 answers)
Screen -X isn't working ("No screen found")
(2 answers)
Closed 6 years ago.
I know that this question has been discussed before but i still can't find a solution. I'm trying to execute a Bash script from a PHP file on a (really small) website which is run using the PHP built in webserver. When running the PHP file in the terminal with
php -f test.php
it works fine and the bash script executes. However when i start the webserver and call the PHP function from the website, nothing happens. The Bash script is invoked with:
exec('/home/user/website/bash_script', $out, $return);
inside the PHP-file and i get that return = 1.
On the website the PHP script that executes the bash-script is called by another PHP script through:
<input type="button" value="..." onclick="location='test.php'"/>
I first thought that it was a problem with my permissions, but i added the script-file in visudo to be able to run it as super-user without password but still no luck. I also changed the file permissions to 755.
Here is test.php (which works fine when run through the terminal):
<?php
exec ('/home/user/website/test');
?>
Also i CAN execute exec('cp bla bla2'); from the webserver so theres no problem with exec()
EDIT------------------------------------------------------------------------
The Bash script is sending a command to a different screen and it seems that this is the root of the problem. I think that this can solve my problem:
Screen -X isn't working ("No screen found")

Related

How to run a .sh file with arguments using shell_exec in PHP? [duplicate]

This question already has answers here:
How can I debug exec() problems?
(4 answers)
Closed 3 years ago.
attempting to run a shell script using PHP. the shell script contains:
'cd /data/nfsshare; python /data/nfsshare/kron_hull.py> /data/nfsshare/log.txt'
that is running a python script named kron_hull.py
the php code im using is:
if ($_POST['visual']) {
shell_exec("sh test.sh");
echo "working! woot woot";
} else {
echo "not working";
}
i am using the echo's as a way to tell if its running the if statement correctly, and i am able to successfully echo "working! woot woot", but the shell file does not run, because the script is meant to create a new file on the server, which it does not.
the shell file works when you put it directly in the linux terminal as "sh test.sh" so the issue is not with the shell file.
i also ran shell_exec('whoami'); and shell_exec('ls -lart'); and was able to echo both of those, so i don't believe it is a problem with the shell_exec() command.
my problem is: i dont believe the shell script is even running when put into the shell_exec() command, and I dont know a better way to check besides looking for the new file its supposed to create, which i get none.
any help is appreciated, if you need me to do anything for testing, or need more information, lmk and ill be glad to answer.
i checked the related questions and they did not help my case
One way to check whether the shell command executed correctly is to use the exec() function instead. exec() gives you the ability to see the return value and the output in a variable.
You can build a error checking execution like this:
if ($_POST['visual']) {
exec("sh test.sh", $output, $return);
// Return will return non-zero upon an error
if (!$return) {
echo "working! woot woot";
} else {
echo "Error with shell script";
}
}
To see error codes you can look here.

'php is not recognized as an internal or external command' even though path is set [duplicate]

This question already has answers here:
‘php.exe’ is not recognized as an internal or external command, operable program or batch file
(10 answers)
Closed 5 years ago.
Guys I have set path of php.exe in environment variables. But still it's the same problem in command prompt :
I have even tried with Gitbash or powershell. I genuinely think I have set path correctly and I even tried adding 'php.exe' at last.
I can't get any solutions. Please HELP.
Try the following :
Before setting the path in your environment variables, test the command directly by navigating to the folder from your command prompt then execute the command .
If the command is working, add the path to your environment variables and restart your computer. Open the command prompt as an administrator by right clicking the command prompt.
Ensure you open the command prompt as an administrator.
Hope this help

Can't execute yolo/darknet from php exec - predictions.png never appears?

YOLO: https://github.com/pjreddie/darknet
I'm trying to run this from within a php script, but predictions.png never appears. I assume this is a general problem with my understanding of how exec works, rather than how YOLO works.
I've changed the permissions and users on all relevant folders and directories to nginx/777. I know that's not ideal but it's a troubleshooting step!
What happens when calling darknet from the terminal:
Logging output appears on the screen, and at the end of the process a file called predictions.php should be created in the /darknet directory. When running this from PHP in the browser I don't care about the logging output, I just want the png file to be created.
Works:
/darknet/darknet detect /darknet/cfg/yolo.cfg /darknet/yolo.weights /path/to/photo.jpg
Works:
sudo -u nginx php71 -r 'exec("/darknet/darknet detect /darknet/cfg/yolo.cfg /darknet/yolo.weights /path/to/photo.jpg");'
Works from commandline (php71 myfile.php), but not in the browser:
<?php
exec("/darknet/darknet detect /darknet/cfg/yolo.cfg /darknet/yolo.weights /path/to/photo.jpg");
What am I doing wrong?

Script_exec() not working when given sh file or command

I'm trying to run a shell script (starting a raspberry pi camera following the second answer from this question - raspivid -o - -t 0 -hf -w 640 -h 360 -fps 25 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554}' :demux=h264), and I'm executing it from a PHP file. When I try to run the script though, it doesn't work.
Running the script normally either by putting it in an .sh file or just entering the command into the shell both work to turn the camera on. When I put shell_exec('pwd'); in the PHP file I get the directory back but when I try to run the the camera script either by shell_exec('[script here]'); or shell_exec('sh path/to/script.sh'); (or system([script]) or putting the script in backticks), nothing happens.
I saw that some people had the same problem but the reason was that PHP could not access a specific directory, which is not a problem here since I can run a test script in the same way - it seems as if this is might be a problem with the specific script i'm running, but I can't imagine why, if it runs fine outside of shell_exec(...);
Does anyone have any insight as to what might be wrong? Much Thanks!

How to run a background process in PHP and get the return value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make PHP wait for Matlab script to finish executing
Okay, starting from php execute a background process
to run a background process works great. The problem is, I need the return of that process also. The obvious solution to me is :
$cmd = "($cmd > $outputfile 2>&1 || echo $? > $returnfile) & echo $! > $pidfile";
exec($cmd);
When I run the generated command on the command line, it backgrounds and the files are filled out as expected. The problem is that when php exec() runs, the command doesn't go to the background (at least, exec doesn't return until the command finishes). I tried variations with nohup and wait $pid, but still no solution.
Any thoughts?
This is tricky- you could potentially fork the process to do something else, leaving the original process in place.
http://php.net/manual/en/function.pcntl-fork.php
However, if this is a web application, there's no built-in way to retrieve the return code or STDOUT back into the parent process since it's technically async (your request-response cycle will likely end before a result can be produced).
You could store the return code and / or STDOUT to files to check later, though.

Categories