Asked  7 Months ago    Answers:  5   Viewed   32 times

I'm trying to make a PHP script, I have the script finished but it takes like 10 minutes to finish the process it is designed to do. This is not a problem, however I presume I have to keep the page loaded all this time which is annoying. Can I have it so that I start the process and then come back 10mins later and just view the log file it has generated?

 Answers

61

Well, you can use "ignore_user_abort(true)"

So the script will continue to work (keep an eye on script duration, perhaps add "set_time_limit(0)")

But a warning here: You will not be able to stop a script with these two lines:

ignore_user_abort(true); 
set_time_limit(0);

Except you can directly access the server and kill the process there! (Been there, done an endless loop, calling itself over and over again, made the server come to a screeching stop, got shouted at...)

Wednesday, March 31, 2021
 
yosemite
answered 7 Months ago
43

You have to reroute programs output somewhere too, usually /dev/null

exec($cmd . " > /dev/null &");
Wednesday, March 31, 2021
 
chugadie
answered 7 Months ago
75

Do you see anything obviously wrong with it?

Nope, the code looks good.

Are there any other paths to the php5 binary that you know of or know of a better order to have the array for optimization?

This is a hard question to answer, as PHP could be installed anywhere on a server. The paths you have seem to be very logical to me, but there could be any number of other places it could be installed.

Rather than providing a bunch of directories where PHP5 might be installed, what about having a parameter the user has to set to provide the path to the PHP5 executable if it's not in their $PATH?

If a host has exec or shell_exec disabled, will the EGalleryProcessQueue.php script be able to be run via a cron job?

I haven't tested it, but I would assume that would prevent the script from running.

Does anyone have any suggestions as to a way in which I can get some feedback as to how far through the images the processing is? See the TODO section of EGalleryProcessQueue.php I'd like to display a progress bar when it's in the admin section.

Store the number of images completed somewhere (file, db, maybe even session var) and have an AJAX call fire every second or so to a function that provides done vs total. Then use something like http://docs.jquery.com/UI/Progressbar

Wednesday, March 31, 2021
 
sassy_geekette
answered 7 Months ago
65

On linux box only:

result.php

<?php
$cmd = 'for i in {1..5}; do date; sleep 2; done';
$outputfile = '/tmp/result.txt';
$pidfile = '/tmp/result.pid';
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
?>

in command line:

php result.php;
tail /tmp/result.txt; /* to monitor the results, take 10 seconds to complete */
cat /tmp/result.pid   /* the pid registered */
Saturday, May 29, 2021
 
Corsair
answered 5 Months ago
80

It's not possible use a PHP file served by a HTTP Server because all proceses will die after the request end.

So cPanel cron is a good option, you don't need to have the cron entry always enable, just setup once the entry and disable it after the script start to run.

Only you'll need add & at the end of your cron command to make it run as background.

Saturday, May 29, 2021
 
barden
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :