Getting script exit status code in php - php

Is there anyway to get the exit status code for a php script run in the background via exec($cmd, $output, $exitCode)?
For example:
exec('php script.php &', $output, $exitCode);
If the trailing '&' isn't included then $exitCode works as expected, it's always 0 otherwise.

For anybody that finds themselves here, my solution was to make a php script that takes a script as an argument. The new script is called to the background and handles exit statuses appropriately.
For example:
$cmd = 'php asynchronous_script.php -p 1';
exec("php script_executor.php -c'$cmd' &");
The second script looks something like
$opts = getOpt('c:');
$cmd = rtrim($opts['c'], '&');
$exitCode = 0;
$output = '';
exec($cmd, $output, $exitCode);
if ($exitCode > 0) {
...
}

I found something that is probably quite equivalent to Pradeep's solution. Someone posted about this on the function's documentation page.
http://www.php.net/manual/en/function.exec.php#101506

Related

How to run a background process by PHP in windows?

I try to run a Python script as a background process from PHP on Windows using exec() on this way:
<?PHP
$python = 'C:\\Users\\User\\anaconda3\\python.exe';
$py_script = 'C:\\wamp\\www\\lab\\ex\\simple_test.py';
$py_stdout = '> temp\\'.session_id()."_std.txt";
$py_stderror = '2> temp\\'.session_id()."_stde.txt";
exec("$py_bg $python $py_script $py_stdout $py_stderror &");
The script called and worked correctly, but PHP still waiting for the script.
I removed the end & as I foundout it's only work on Linux and after searching other Q&A find this sulotion:
exec("start /B $py_bg $python $py_script $py_stdout $py_stderror");
But same result. How can I solve this problem?
=== UPDATE:
I used start /B in the wrong way, I changed my code to this:
<?PHP
$python = 'C:\\Users\\User\\anaconda3\\python.exe';
$py_script = 'C:\\wamp\\www\\lab\\ex\\simple_test.py';
$py_stdout = '> temp\\'.session_id()."_std.txt";
$py_stderror = '2> temp\\'.session_id()."_stde.txt";
$py_cmd = "$python $py_script $py_arg_1 $py_std $py_stde";
pclose(popen("start /B ". $py_cmd, "a"));
But now a Warning in PHP for popen():
Warning: popen(start /B ...,a): No error in C:\wamp\www\lab\start.php on line 50
and an other for pclose():
Warning: pclose() expects parameter 1 to be resource, bool given in ...
I checked PHP: popen - Manual and see there a is not a valid mode, but I see this on several answers around here!
however:
The mode. Either 'r' for reading, or 'w' for writing.
By changing mode to r, the script call and run in the background correctly and there is not an error or warning on PHP or Py.
<?PHP
$python = 'C:\\Users\\User\\anaconda3\\python.exe';
$py_script = 'C:\\wamp\\www\\lab\\ex\\simple_test.py';
$py_stdout = '> temp\\'.session_id()."_std.txt";
$py_stderror = '2> temp\\'.session_id()."_stde.txt";
$py_cmd = "$python $py_script $py_arg_1 $py_std $py_stde";
pclose(popen("start /B ". $py_cmd, "r"));

dynamically get results from exec

I have a php script that calls a go script. It gets results every 1-2 seconds, and print's them. Using php's exec and output, I only get the results when the program finishes. Is there a way I can check the output to see when it changes and output that while it's still running?
Something like this, but pausing the execution?:
$return_status = 0;
$output = [];
$old_output = ["SOMETHING ELSE"];
while ($return_status == 0) {
exec($my_program,$output,$return_status); #somehow pause this?
if $output != $old_output {
echo($output);
$old_output = $output;
}
}
Yes. Use the popen() function to get a file handle for the command's output, then read from it a line at a time.

How to find out the process or script which call php bash script

I have php bash script that do some database processing. All I need to know is the caller of this script. I don't know if it's process or other script. So is there some way to know process id or script name of caller?
Script is running by some process and its code starts with interpreter path "#!/usr/bin/php". This file called as bash script only.
OS: Centos 6.5
You can try something like this
<?php
Check for a current process by filename
function processExists($file = false) {
$exists = false;
$file = $file ? $file : __FILE__;
// Check if file is in process list
exec("ps -C $file -o pid=", $pids);
if (count($pids) > 1) {
$exists = true;
}
return $exists;
}
?>
This will check against the filename that you're trying to track. If you need the process id, just adjust what's in in the exec or return the $pids.
try this ---
$mystring = "script_running";
exec("ps aux | grep \"${mystring}\" | grep -v grep | awk '{ print $2 }' | head -1", $out);
print "The PID is: " . $out[0];
ps aux is more descriptive of what's running.

Creating a PHP Online Grading System on Linux: exec Behavior, Process IDs, and grep

Background
I am writing a simple online judge (a code grading system) using PHP and MySQL. It takes submitted codes in C++ and Java, compiles them, and tests them.
This is Apache running PHP 5.2 on an old version of Ubuntu.
What I am currently doing
I have a php program that loops infinitely, calling another php program by
//for(infinity)
exec("php -f grade.php");
//...
every tenth of a second. Let's call the first one looper.php and the second one grade.php. (Checkpoint: grade.php should completely finish running before the "for" loop continues, correct?)
grade.php pulls the earliest submitted code that needs to be graded from the MySQL database, puts that code in a file (test.[cpp/java]), and calls 2 other php programs in succession, named compile.php and test.php, like so:
//...
exec("php -f compile.php");
//...
//for([all tests])
exec("php -f test.php");
//...
(Checkpoint: compile.php should completely finish running before the "for" loop calling test.php even starts, correct?)
compile.php then compiles the program in test.[cpp/java] as a background process. For now, let's assume that it's compiling a Java program and that test.java is located in a subdirectory. I now have
//...
//$dir = "./sub/" or some other subdirectory; this may be an absolute path
$start_time = microtime(true); //to get elapsed compilation time later
exec("javac ".$dir."test.java -d ".$dir." 2> ".$dir
."compileError.txt 1> ".$dir."compileText.txt & echo $!", $out);
//...
in compile.php. It's redirecting the output from javac, so javac should be running as a background process... and it seems like it works. The $out should be grabbing the process id of javac in $out[0].
The real problem
I want to stop compiling if for some reason compiling takes more than 10 seconds, and I want to end compile.php if the program stops compiling before 10 seconds. Since the exec("javac... I called above is a background process (or is it?), I have no way of knowing when it has completed without looking at the process id, which should have been stored in $out earlier. Right after, in compile.php, I do this with a 10 second loop calling exec("ps ax | grep [pid].*javac"); and seeing if the pid still exists:
//...
$pid = (int)$out[0];
$done_compile = false;
while((microtime(true) - $start_time < 10) && !$done_compile) {
usleep(20000); // only sleep 0.02 seconds between checks
unset($grep);
exec("ps ax | grep ".$pid.".*javac", $grep);
$found_process = false;
//loop through the results from grep
while(!$found_process && list(, $proc) = each($grep)) {
$boom = explode(" ", $proc);
$npid = (int)$boom[0];
if($npid == $pid)
$found_process = true;
}
$done_compile = !$found_process;
}
if(!done_compile)
exec("kill -9 ".$pid);
//...
... which doesn't seem to be working. At least some of the time. Often, what happens is test.php starts running before the javac even stops, resulting in test.php not being able to find the main class when it tries to run the java program. I think that the loop is bypassed for some reason, though this may not be the case. At other times, the entire grading system works as intended.
Meanwhile, test.php also uses the same strategy (with the X-second loop and the grep) in running a program in a certain time limit, and it has a similar bug.
I think the bug lies in the grep not finding javac's pid even when javac is still running, resulting in the 10 second loop breaking early. Can you spot an obvious bug? A more discreet bug? Is there a problem with my usage of exec? Is there a problem with $out? Or is something entirely different happening?
Thank you for reading my long question. All help is appreciated.
I just came up with this code that will run a process, and terminate it if it runs longer than $timeout seconds. If it terminates before the timeout, it will have the program output in $output and the exit status in $return_value.
I have tested it and it seems to work well. Hopefully you can adapt it to your needs.
<?php
$command = 'echo Hello; sleep 30'; // the command to execute
$timeout = 5; // terminate process if it goes longer than this time in seconds
$cwd = '/tmp'; // working directory of executing process
$env = null; // environment variables to set, null to use same as PHP
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
// start the process
$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
$startTime = time();
$terminated = false;
$output = '';
if (is_resource($process)) {
// process was started
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
// loop infinitely until timeout, or process finishes
for(;;) {
usleep(100000); // dont consume too many resources
$stat = proc_get_status($process); // get info on process
if ($stat['running']) { // still running
if (time() - $startTime > $timeout) { // check for timeout
// close descriptors
fclose($pipes[1]);
fclose($pipes[0]);
proc_terminate($process); // terminate process
$return_value = proc_close($process); // get return value
$terminated = true;
break;
}
} else {
// process finished before timeout
$output = stream_get_contents($pipes[1]); // get output of command
// close descriptors
fclose($pipes[1]);
fclose($pipes[0]);
proc_close($process); // close process
$return_value = $stat['exitcode']; // set exit code
break;
}
}
if (!$terminated) {
echo $output;
}
echo "command returned $return_value\n";
if ($terminated) echo "Process was terminated due to long execution\n";
} else {
echo "Failed to start process!\n";
}
References: proc_open(), proc_close(), proc_get_status(), proc_terminate()

ffmpeg php exec not working

I have ffmpeg installed
safe_mode is off
when i do this: $movie = new ffmpeg_movie('Bear.wmv');
I can use getDuration(), getFilename().... wthout any problems, so it's all seems to be working
exec is working fine, cos when i do: $output = exec('ls -lart'); I get a nice little result.
but when I do this:
exec('ffmpeg -i Bear.wmv outputfile.flv')
nothing happens
if I add: $command_output, $result
the only result i guess is: array { }
I have tried everything I could think of:
exec('ffmpeg -i Bear.wmv outputfile.flv')
exec('ffmpeg.so -i Bear.wmv outputfile.flv')
exec('/usr/lib/php5/20090626/ffmpeg -i Bear.wmv outputfile.flv')
I've tried all sizes and folders and codecs but still don't get anything back
All i wanna do is convert that video, Bear.wmv to an flv file.
I'm very close to crying like a baby and/or jumping out of the window (im only on the first floor but still lol)
so Please help!!??!
FFMPEG is a application wich don't output to STDIO but STDERR, so you can redirect it to standard output:
$cmd = $FFMPEGDIR . " -i somefile.avi 2>&1"; // SEE 2>&1 !!
Extracting size:
exec( $cmd , $info );
echo "<pre>".print_r($info,true)."</pre>";
$resolution = preg_match( '#[ ,\t]([0-9]{3,4}x[0-9]{3,4})[ ,\t]#si' , implode( " " , $info ) , $durmatches );
$rtab = explode( "x" , $durmatches[1] );
$videowidth = $rtab[0];
$videoheight = $rtab[1];
Recently set ffmpeg up for audio stuff... it's a bit of a black art, ffmpeg is notorious for not playing nice (or consistently) - what works (worked) for me might not work for you!
try using: shell_exec()
or:
$command="{$FFMPEG_BINARY} ... rest of your options";
$process=proc_open($command, $descriptors, $pipes);
if (!$process)
{
// failed to exec...
}
else
{
// command ran...
}
my ffmpeg was in... "/usr/bin/ffmpeg" just check you've got right path too.

Categories