1

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.

3
  • So what you want to do is monitor the progress of the task? Commented Feb 15, 2012 at 17:42
  • Yes - I believe so, and ensure that the process remains running even if the browser is closed. Commented Feb 15, 2012 at 17:43
  • You can use ajax to do that described in this thread: stackoverflow.com/questions/7049303/… Commented Feb 15, 2012 at 17:45

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Do I use exec() or system() to set up the server process? Is this what you mean?
Are you using shared hosting? If so, here is how you can setup a cron job in cPanel. Other shared hosting will have a similar way of going about it. upstartblogger.com/how-to-create-a-cron-job-in-cpanel
The script running each time is dynamic, I want to be able to do it directly from PHP, not by setting up cron jobs through a panel. No I am on a dedicated server not shared hosting.
It's still possible, even if it is dynamic. Maybe this will help more? net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.