I am running some php exec commands on PHP - CLI
Some of these exec() take too long.
So my idea is to setup a 60 seconds timeout on the exec()
I found some solutions for Linux, but I could not adapt them on windows (pipe/processes...)
Any idea on how to trigger a timeout on windows php cli exec() command ?
$intExecutionTimeout = 60;
$strCommand = 'wget http://google.com';
$strCommand = 'timeout --signal=15 --kill-after=' . ( $intExecutionTimeout* 2 ) . 's ' . $intExecutionTimeout . 's ' . $strCommand . ' &';
exec( $strCommand, $arrstrResponse );
try timeout command in CLI:
$time = 60;
$command = 'wget http://google.com';
exec(sprintf("C:\Windows\System32\timeout.exe /t %d %s", $time, $command), $output);
Related
I'm using Windows Server 2016
This is the PHP code I use:
exec('"' . $ffmpeg . '" -y -i "' . $inputFile . '" -vcodec copy -acodec copy -ss 00:00:00 -to ' . $till . ' "' . $temp . '" 2>&1', $output, $return_var);
It works but after that I get 500 Internal Server Error which lasts about 2 - 3 seconds.
I tried to change 2>&1 to > /dev/null 2>&1 & based on this answer so:
exec('"' . $ffmpeg . '" -y -i "' . $inputFile . '" -vcodec copy -acodec copy -ss 00:00:00 -to ' . $till . ' "' . $temp . '" > /dev/null 2>&1 &', $output, $return_var);
but now the conversion doesn't work any more.
EDIT:
I should add that the 500 Internal Server Error doesn't happen when I run the script but when I open the website within 2 - 3 seconds after conversion
Hello I'am trying to execute a shell command in my php script but it is not working.
My php script :
//I save the Order
$holdedOrder->save();
$id = $holdedOrder->id;
$old_path = getcwd();
chdir(__DIR__.'/../');
$scriptFile = 'anacron_job_unhold_order.sh';
$bool = file_exists($scriptFile);
//$bool is true !
//this command works in shell but not in here do not know why
$s = shell_exec("echo \"/usr/bin/bash $scriptFile $id\" | /usr/bin/at now +$when");
chdir($old_path);
return [$s,$bool];
$when has a valid value 4 hours or 4 days ...
The command will be :
echo bash anacron_job_unhold_order.sh 29 | at now +1 minutes
the output is null. Trying it with exec() is returning 127 code
Edit :
I removed www-data from /etc/at.deny and still the same problem
The command shell_exec("echo \"/usr/bin/bash $scriptFile $id\" | /usr/bin/at now +$when"); is probably causing the issue, you should take a closer look on how at works.
The command should be something like
at [options] [job...]
To give a job to at from the command instead of STDIN, use heredoc syntax
at now + 1 minute <<< 'touch /tmp/test'
So the PHP code should be something like;
$s = shell_exec("/usr/bin/at now + {$when} <<< '/usr/bin/bash {$scriptFile} {$id}'");
exec($s, $output, $return_var);
Server Information:
CentOS 6.5
12GB RAM
Intel(R) Xeon(R) CPU E5-2430 # 6 CPU x 2.20GHz
PHP CLI 5.5.7
I am currently trying to use Perl to fire off 1000 PHP CLI processes in parallel. This however takes 9.9 seconds vs 2.3 seconds for the equivalent Perl script. When I test using the Perl script /opt/test.pl, all 1000 processes are launched in parallel (ps -eLf | grep -ic 'test.pl'). When I test using /opt/testphp.php, using ps -eLf | grep -ic 'testphp.php', I see a count of 250, then it rises to 580 and then it drops to 0 (the script is executed 1000 times, just not in parallel).
Is there a limitation preventing a high number of PHP CLI processes from being launched in parallel?
Has anyone experienced this issue?
Please let me know if I have left out anything that would help to identify the issue.
Thanks
Perl launcher script:
use Time::HiRes qw/ time sleep /;
my $command = '';
my $start = time;
my $filename = '/tmp/report.txt';
# open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
for $i(1 .. 1000) {
# $command = $command . "(perl /opt/test.pl &);"; // takes 2.3 seconds
$command = $command . "(php -q /opt/testphp.php &);"; // takes 9.9 seconds
}
system($command);
my $end = time;
print 'Total time taken: ', ( $end - $start ) , "\n";
PHP file (testphp.php):
sleep(5);
$time = microtime(true);
file_put_contents('/tmp/report_20140804_php.log', "This is the record: $time\n", FILE_APPEND);
Perl file (test.pl):
#! /usr/bin/perl
use Time::HiRes qw/ time sleep /;
sleep(5);
my $command = '';
my $start = time;
my $filename = '/tmp/report_20140804.log';
open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
print $fh "Successfully saved entry $start\n";
close $fh;
I need to print a (custom) number of copies on a DYMO-450 labelprinter, using a apache based local linux server, running php, wkhtmltopdf, xvfb and stuff...
If have trouble with giving a number of copies to the print job. Normally it shout be done with
lp -d PRINTER DOCUMENT -n2 // or possibly -n 2
for 2 copies.
But the DYMO doesn't.
Currently I am am using this workaround, which does the job, but I am waiting up to 3 seconds between each printed label:
$printcmd = '';
for ( $p=0; $p < $_REQ['copies'] ; $p++ ) {
if ($p>0) $printcmd .= '&& ';
$printcmd .= 'lp -d ' . $cfg['labelprinter'] . ' ' . $pdf_file . ' > print.log 2>&1';
}
system($printcmd . ' &');
But this doesn't suck at all.
Any suggestions ?
The (late) answer is, adding -o Collate=True
lp -n num-copies -o Collate=True filename
thanks to http://www.cups.org/documentation.php/options.html
From a PHP script I need to get the last system boot time, preferably in the UNIX time stamp format.
Are there any files that are created or modified at boot time? I could read the modified time of the file.
The system type is CentOS.
/proc/uptime
This file contains two numbers: the uptime of the system (seconds), and the amount of time spent in idle process (seconds).
<?php
function get_boottime() {
$tmp = explode(' ', file_get_contents('/proc/uptime'));
return time() - intval($tmp[0]);
}
echo get_boottime() . "\n";
Try this:
Method 1
<?php
$uptime = trim( shell_exec( 'uptime' ) );
// output is 04:47:32 up 187 days, 5:03, 1 user, load average: 0.55, 0.55, 0.54
$uptime = explode( ',', $uptime );
$uptime = explode( ' ', $uptime[0] );
$uptime = $uptime[2] . ' ' . $uptime[3]; // 187 days
?>
Method 2
<?php
$uptime = trim( file_get_contents( '/proc/uptime' ) );
$uptime = explode( ' ', $uptime[0] );
echo $uptime[0];//uptime in seconds
?>
Hope it helps.
who -b # in command-line
Gives you the last boot time (http://www.kernelhardware.org/find-last-reboot-time-and-date-on-linux/)
exec ( $command , $output ); // in PHP
Executes a shell command
strtotime(); // in PHP
Parse about any English textual datetime description into a Unix timestamp
So this will give you the Linux System last boot time :
// in PHP
exec('who -b',$bootTime);//where $bootTime is an array, each line returned by the command is an element of the array
$result = strtotime($bootTime[1]);