I have a function that should run a process in background
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen('start /B '.$cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
I try to run script
$cmd = "php ..\runffmpeg.php";
execInBackground($cmd);
But it does nothing. When I try to run
$cmd = 'ffmpeg -i video_in.mp4 video_out.avi';
execInBackground($cmd);
Its all right. And when I try to run
exec("php ..\runffmpeg.php");
It's also all right.
So, pclose(popen('start /B php ..\runffmpeg.php', "r")); doesn't run a command. Whats a problem?
I'm using Windows and php 5.4.7
You can use: $cmd= include("php ..\runffmpeg.php");
hmm; forget process mgmt and just popen() the resource.
If it is a daemon, you get access to it, else a subprocess is created/destroyed with the popen()/pclose().
Related
im trying to execute an py file from php, using bat file .
the whole code works fine and im trying to execute an file name dbConn.py
the file executes when seperatly double clicked i.e executed and output is reflected
the code from php is this:
if ($conn->query($sql) === TRUE) {
mysqli_close($conn);
$cmd = "run_dbConn.bat" ;
execInBackground($cmd);
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
This is code for runcmd.php
<?php
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
?>
and code for run_dbConn.bat
cd C:\xampp\htdocs\eclipse_workspace\Project
python dbConn.py
ECHO %1
echo
even the above code make the file to run , which i have checked from task bar. but the output is not generated or reflected. the dbConn.py does not have something related to code and can be executed separately....
plzz help me out even a suggestion will be well.
exec($cmd . " > /dev/null &");
This line will run the command and redirect the output to a null device, aka hides the output.
Try removing
> /dev/null
am executing a shell command like below.
shell_exec('java -jar sanityTest.jar');
$success = array('status' => "Success",'type' => "execute");
echo json_encode($success);
The shell_exec command not going to next statement until the execution complete. What I want is to execute it in background, even for Windows.
I tried
shell_exec('java -jar sanityTest.jar >/dev/null 2>/dev/null &');
which is coming to next line but not executing the command.
My solution: use start /B *my command*
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
} else {
exec($cmd . " > /dev/null &");
}
}
I could have found it by looking at the right keywords: https://stackoverflow.com/a/21031259/6019417
I am using the following code to run the function in background.
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null 2>&1 &");
}
}
$cmd = execInBackground("http://example.com/parentdesc/frontend/web/index.php?r=site%2Ftest");
It is working fine. But how can I pass the parameters to the exec?
I tried:
$cmd = execInBackground("http://example.com/parentdesc/frontend/web/index.php?r=site%2Ftest \"param1\" \"param2\" ");
And in my function I print the $argv. But it doesn't have any values in it.
My function:
public function actionTest()
{
// does not have anything
print_r($argv);
// here is my mail sending logic
}
Am I missing something?
am using windows-7 OS and wamp server.
i have 2 php files trigger.php,background.php.
i want to run background.php in background .i have to call this file from trigger.php .
i tried below methods.
i added this code in trigger.php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:\wamp\bin\php\php5.3.5\php-win.exe -f C:/wamp/www/background.php", 0, false);
but my background.php is not getting called.
how i can do this?
any suggestions are appreciated.
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
in this case your $cmd would be "php C:/wamp/www/path/to/background.php"
i changed the function as below,
$cmd = 'C:\wamp\bin\php\php5.3.5\php.exe C:\wamp\www\email3.php';
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B " . $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
and , its works for me. :)
Update:
function execInBackground(string $cmd)
{
if (substr(php_uname(), 0, 7) == "Windows") {
pclose(popen("start /B {$cmd}", "r"));
return;
}
exec("{$cmd} > /dev/null &");
}
$phpBinPath = defined('PHP_BINARY') ? PHP_BINARY : '/usr/bin/php';
$fileToRun = __DIR__ . '/sleep.php';
$cmd = "'{$phpBinPath}' '{$fileToRun}'";
execInBackground($cmd);
echo 'Executou!!!';
I have an SSH command that I'd like to execute with libssh2 in PHP:
sh -c '
rm -f /tmp/command.log
sleep 3 &
screen -p 0 -X stuff "\
script -a -c \"ls -l\" /tmp/command.log; kill -USR1 $!
"
wait
cat /tmp/command.log
'
I can't seem to escape it properly though, so SSH receives it exactly as above. I need to wrap it in double quotes so I can get PHP variables in there as well (ls -l will become $command).
I have tried:
"sh -c '
rm -f /tmp/command.log
sleep 3 &
screen -p 0 -X stuff \"\
script -a -c \\"ls -l\\" /tmp/command.log; kill -USR1 $!
\"
wait
cat /tmp/command.log
'"
as well as:
"sh -c '
rm -f /tmp/command.log
sleep 3 &
screen -p 0 -X stuff \"\
script -a -c \\\"ls -l\\\" /tmp/command.log; kill -USR1 $!
\"
wait
cat /tmp/command.log
'"
The first of which returns a PHP error and the second of which doesn't run the command.
The whole function (after the edit Morgan Wilde suggested):
function runShellCommand($command, $host, $user, $pass, $port){
if (!function_exists("ssh2_connect")) die("Fail: function ssh2_connect doesn't exist");
if(!($con = ssh2_connect($host, $port))){
return "Unable to establish connection. Is your server offline?";
} else {
if(!ssh2_auth_password($con, $user, $pass)) {
return "Failed to authenticate. Please ensure your server's password matches our records.";
} else {
$run = <<<HEREDOC
sh -c '
rm -f /tmp/command.log
sleep 3 &
screen -p 0 -X stuff "\
script -a -c \"touch /root/test234\" /tmp/command.log; kill -USR1 $!
"
wait
cat /tmp/command.log
'
HEREDOC;
if (!($stream = ssh2_exec($con, $run ))) {
return "Could not run command.";
} else {
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
fclose($stream);
if(empty($data)){
return "sh-4.1# $command\n\n";
} else {
return "sh-4.1# $command\n$data\n";
}
}
}
}
}
How about using the HEREDOC string quoting? I haven't tried it, but it works for other use cases.
$command = <<<HEREDOC
sh -c '
rm -f /tmp/command.log
sleep 3 &
screen -p 0 -X stuff "\
script -a -c \"ls -l\" /tmp/command.log; kill -USR1 $!
"
wait
cat /tmp/command.log
'
HEREDOC;
More on that here - http://php.net/manual/en/language.types.string.php
Try phpseclib, a pure PHP SSH implementation. eg.
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->read('username#username:~$');
$ssh->write("rm -f /tmp/command.log\n");
echo $ssh->read('username#username:~$');
$ssh->write("sleep 3 &\n");
echo $ssh->read('username#username:~$');
$ssh->write("screen -p 0 -X stuff \"\
script -a -c \\\"ls -l\\\" /tmp/command.log; kill -USR1 $!
\"");
...
?>