简体   繁体   中英

PHP : How can I ensure my script is long running?

I am developing a simple application which will allow me to migrate emails from one IMAP server to another.

Although the script works - it may be the case that some IMAP Mailboxes are very large, and the script is running for a long time. I know that I can set the time limit of my script to never end using : set_time_limit(0)

However ideally I would like to be able to set it to start and be able to view the webpage later and see if it is still running or finished. Is there a way of starting a process and then coming back to view that same process?

I'm not really sure of the right terminology to when asking a question like this, so I hope I have made myself clear :)

EDIT: I've just seen the function ignore_user_abort() but I'm not sure how this will allow me to come back to the same page and see if the process is still running, stopped due to an error, or whether it has finished.

Setup a cron job to run your script and have it log it's status to a text log somewhere. Then all you have to do is check your logs.

In a normal HTTP request, PHP is run as a sub process to some other program. Typically Apache, but it's all a matter of what your web server is. The problem is that, even with the options in PHP there's always a chance the web server will end the PHP process for a variety of different reasons.

The best solution to having a long running PHP script is to run the script through the Command Line Interface (CLI) http://php.net/manual/en/features.commandline.php

There it can run for as long as it wants. I would still use the set_time_limit(0) but check the link above for the specifics.

Also, if you don't have access to the command line on your server, typically you'll have access to some sort of task scheduler (CRON in Linux). These typically work by executing a command as if it were on the command line, so you could setup a task to run the PHP script on demand.

As for the status updates. You can have the migration code write the status to a database, or text file. Then you create a second, normal PHP page that displays the status updates.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM