Run a command line using PHP - 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

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.

Executing a shell script in background with php

I need to execute a shell script. The catch is I want to do this
$Command = "nohup cvlc input --sout '#transcode {vcodec=h264,acodec=mp3,samplerate=44100}:std{access=http,mux=ffmpeg{mux=flv},dst=0.0.0.0:8083/".output"}' &";
$str = shell_exec($Command);
I dont want it to wait till the command is finished, i want it to run in a background process.
I do not want another php thread as it will timeout the command can take up to 3 hours to finish.
$str = shell_exec($Command.' 2>&1 > out.log');
You need to redirect the output of the command.
If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
http://php.net/manual/en/function.exec.php
You can try running your command in background using a function like this one:
function exec_bg($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
This makes your shell command runs, but the php flow continues.

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 &");
}
}

php exec command (or similar) to not wait for result

I have a command I want to run, but I do not want PHP to sit and wait for the result.
<?php
echo "Starting Script";
exec('run_baby_run');
echo "Thanks, Script is running in background";
?>
Is it possible to have PHP not wait for the result.. i.e. just kick it off and move along to the next command.
I cant find anything, and not sure its even possible. The best I could find was someone making a CRON job to start in a minute.
From the documentation:
In order to execute a command and have it not hang your PHP script while
it runs, the program you run must not output back to PHP. To do this,
redirect both stdout and stderr to /dev/null, then background it.
> /dev/null 2>&1 &
In order to execute a command and have
it spawned off as another process that
is not dependent on the Apache thread
to keep running (will not die if
somebody cancels the page) run this:
exec('bash -c "exec nohup setsid your_command > /dev/null 2>&1 &"');
You can run the command in the background by adding a & at the end of it as:
exec('run_baby_run &');
But doing this alone will hang your script because:
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
So you can redirect the stdout of the command to a file, if you want to see it later or to /dev/null if you want to discard it as:
exec('run_baby_run > /dev/null &');
This uses wget to notify a URL of something without waiting.
$command = 'wget -qO- http://test.com/data=data';
exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid);
This uses ls to update a log without waiting.
$command = 'ls -la > content.log';
exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid);
I know this question has been answered but the answers i found here didn't work for my scenario ( or for Windows ).
I am using windows 10 laptop with PHP 7.2 in Xampp v3.2.4.
$command = 'php Cron.php send_email "'. $id .'"';
if ( substr(php_uname(), 0, 7) == "Windows" )
{
//windows
pclose(popen("start /B " . $command . " 1> temp/update_log 2>&1 &", "r"));
}
else
{
//linux
shell_exec( $command . " > /dev/null 2>&1 &" );
}
This worked perfectly for me.
I hope it will help someone with windows. Cheers.
There are two possible ways to implement it.
The easiest way is direct result to dev/null
exec("run_baby_run > /dev/null 2>&1 &");
But in case you have any other operations to be performed you may consider ignore_user_abort
In this case the script will be running even after you close connection.
"exec nohup setsid your_command"
the nohup allows your_command to continue even though the process that launched may terminate first. If it does, the the SIGNUP signal will be sent to your_command causing it to terminate (unless it catches that signal and ignores it).
On Windows, you may use the COM object:
if(class_exists('COM')) {
$shell = new COM('WScript.Shell');
$shell->Run($cmd, 1, false);
}
else {
exec('nohup ' . $cmd . ' 2>&1 &');
}

Categories