I have a PHP script editxml.php
that combines an array of XML files using a couple foreach loops. It works fines with 4 or 5 files but if I try 20 files then the server times out and I receive a 503 error. The script outputs one XML file that is saved in the same directory, but nothing needs to be visible in the browser. How can I run this in the background until it finishes?
$fileout = 'output.xml';
$dst = simplexml_load_file( 'template.xml' );
$dst = dom_import_simplexml( $dst )->ownerDocument;
$parent = $dst->getElementsByTagName( 'root' )->item(0);
$files = array(
"1.xml", // desc
"2.xml", // desc
"3.xml", // desc
"4.xml", // desc
"5.xml", // desc
"6.xml", // desc
);
foreach ($files as $xml) {
$src = simplexml_load_file($xml);
foreach( $src->product as $product )
{
$node = dom_import_simplexml( $product );
$node = $dst->importNode( $node, 1 );
$parent->appendChild( $node );
}
}
$final = new DOMDocument();
$final->loadXML( $dst->saveXML(), LIBXML_NOBLANKS );
$final->formatOutput = True;
echo $final->saveXML();
$fh = fopen( $fileout, 'w') or die ( "can't open file $fileout" );
fwrite( $fh, $final->saveXML() );
fclose( $fh );
I'm using a digitalocean ubuntu 14.04 droplet with serverpilot but anything that requires shell/bash or remote ssh or PATH changes is extremely difficult for me so I'd like to avoid that if possible. I know the path but connecting PHP and shell remotely, etc... is not my strong point.
* 15 * * * /opt/sp/php5.6/bin/php -f /path/to/trigger.php > /dev/null 2>&1
Will running a cron job to a trigger.php
file that triggers the editxml.php
file keep the script running in the background? Inside trigger.php
is: exec("/path/to/editxmltest.php > /dev/null &");
Seems your server getting time-out on execution. Increase your execution time by adding below code at the top of your code
set_time_limit(0);
You can also increase on php.ini ( Refer )
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
Please check on the below thread for best practices,
More information on the fix on this subreddit thread: https://www.reddit.com/r/PHPhelp/comments/4a9l48/best_practices_for_running_a_large_php_script_in/
I ended up manually running it from the command line as a logged in user, I will worry about automating the process later on but the PHP process completed successfully and output the file to the correct directory. The command that I used was
nohup php -q /path/to/test.php > /path/to/process_log.xml 2>&1 &
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.