exec command not running on linux hosting - php

The following code is working on localhost xampp windows, but when i put it online godaddy shared linux hosting its not working. exec is working as i tries exec('whoami') and it outputs my account id, but the following exec command is not executing.
is there any changes i need to make it work? please guide.
$cmd = 'php file.php';
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}

PHP exec are not supported on share hosting accounts: http://support.godaddy.com/groups/web-hosting/forum/topic/is-php-exec-available-or-executable/

Related

How to open cmd by php function on vps

Im trying to run the function
execInBackground('start cmd.exe #cmd /k "ping google.com"');
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
on my vps but it's not working,
but when I run it exactly on my local pc in works fine, cmd open and run "ping google.com" ,what is making it not working on my vps? I added php to enviroment variables too

Call an asyncron php script on a Linux Server

I try to execute a .php script with the shell_exec() function on a 1&1 Linux shared Webserver (debian GNU/Linux 8 distribution).
private function callAsyncSkript(){
// Asynchron Zipping over executing a Script in a new process.
if (substr(php_uname(), 0, 7) == "Windows"){
$cmd = "start /B php .\src\Core\Services\AsynchronZip.php fileids=".$this->idQuery . " fulllink=".$this->uri;
pclose(popen($cmd, "r"));
}
else {
$cmd = "/usr/bin/php7.2-cli ".getcwd()."/src/Core/Services/AsynchronZip.php fileids=".$this->idQuery . " fulllink=".$this->uri;
shell_exec("/usr/bin/nohup ".$cmd." >/dev/null 2>&1 &"." | at now");
//shell_exec("/usr/bin/nohup ".$cmd." >/dev/null 2>&1 &");
}
}
The "/usr/bin/nohup " addition donĀ“t help to execute the script asynchron,
if iam using " | at now" the script will not be executed.
Are there some special settings i have to set or are there other options for an asynchron execution over the commandline?
I found out that the only shell available to me is a rssh. Is it possible that she is the mistake.

Run a command line using PHP

I am trying to run a command in CMD using PHP and get a output in web. After doing some research I came to know about exec('command', $output, $return_var) function. And i got the output when i am running ping www.google.com because after the execution is complete the command is getting end. But what I am looking for when I run the command I need to get the output Node Ready and as the execution is not getting ended and its running that's why server its waiting for the output. Is there anyway where i can get the output in this condition.
My CMD output is :
This function its helping me to run the CMD and execute the command. How will I get the output in the web??
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows") {
pclose(popen("start /B ". $cmd, "r"));
} else {
exec($cmd . " > /dev/null &");
}
}
Thank you

PHP line pclose(popen(... won't work on Amazon AWS with MAMP

I have MAMP installed and am running a php script to execute external programs (jar and exe). Everything works perfectly on my local machine.
However when I put it all on the AWS instance, it just ignores the pclose(popen(...)) line. I get both log messages and no errors.
$command = 'start /B cmd /C '.$this->getCommand().' >NUL 2>NUL';
$this->Log.debug('executing command : '.$command);
pclose(popen($command, 'r'));
$this->Log.debug('command sent');
I'm thinking it might be a permissions issue of some sort? But I've checked the php.ini and the Apache permissions and everything looks good. Php is not being told to ignore the popen command.
Update:
I created a test.bat file in the same location
echo Hello, World > donald.txt
and this executes when called from php, so now I know the pclose(popen()) is working. The command I am trying to execute is java - jar ... so it must be something to do with permissions running java?
So this works:
start /B cmd /C D:/programs/test.bat >NUL 2>NUL
and this does not:
start /B cmd /C java -Xmx2048m -jar "D:\programs\program.jar" -pm XXX_YYY -t FULL -flag1 -flag2 -k 23 >NUL 2>NUL
Remove everything that is unnecessary, to make your command look like this:
$command = 'start /B ' . $this->getCommand();
you don't need any redirection as start /b will start program without new window.
I have the following:
$cmd = 'start /b php {$file} {$bid}';
pclose(popen($cmd, 'r'));
And it works on windows perfectly.
BTW, for Linux-based machine you need to run command like this:
$command = $this->getCommand() . ' &';
to make one script works on windows and Linux you need something like this:
$command = $this->getCommand() . ' &';
if (false !== stripos(PHP_OS, 'win')) {
$command = 'start /B ' . $this->getCommand();
}
$this->Log.debug('executing command : '.$command);
$p = popen($command, 'r');
sleep(10); //we will give some time for process to start
pclose($p);
$this->Log.debug('command sent');
UPDATE: in the chat we've figured out that the problem was with path to java.exe. Apache was unable to find java executable. so the solution was to specify absolute path to java.exe:
$command = 'start /B C:/Progra~1/Java/jre1.8.0_102/bin/java.exe -Xmx2048m -jar "C:/programs/some-jar-file.jar" -?';

Start ffmpeg transcoding with php

Hey there! I want to restart a live stream by hand via a php script. Everything works fine so far, but the following command causes that the script loads forever and the transcoding isn't working:
nohup openRTSP -v -c rtsp://*****.dyndns.org:665 | ffmpeg -r 5 -f mjpeg -i - http://127.0.0.1:8090/feed1.ffm > /dev/null &
Any ideas how to start that command e.g. without waiting for the output?
Not that it will necessarily solve your problem, but it should answer your question, found in the PHP comments under "exec" in which several people came across similar situations.
I combined several efforts in this
topic into one function: This will
execute $cmd in the background (no cmd
window) without PHP waiting for it to
finish, on both Windows and Unix.
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}

Categories