i’ve a php script that runs a database backup and compress it without any particular html output. This script includes a time check to avoid execution before a specified delay stored in db.
Now the problem is that i cannot use the server cronjob tasks because the webserver doesn’t support it.
So i would create a javascript function that should call php script at each visit ( yes it’is enough for my purpose ) but without delaying the user that visit the page, since it requires about 1 minute to end the process.
Is it possible to run a script without waiting for response? a php script can continue to run also when the user session is terminated?
i think something similar to:
http://drupal.org/project/poormanscron
thanks in advance
EDIT:
SOLUTION 1 (php only):
putting this before my output:
public static function cronjob() {
ignore_user_abort(true); // optional
session_write_close();//close session file on server side to avoid blocking other requests
header("Content-Encoding: none");//send header to avoid the browser side to take content as gzip format
header("Content-Length: ".ob_get_length());//send length header
header("Connection: close");//or redirect to some url: header('Location: http://www.google.com');
ob_end_flush();flush();//really send content, can't change the order:1.ob buffer to normal buffer, 2.normal buffer to output
//fastcgi_finish_request(); // important when using php-fpm!
// Do processing here
sleep(5);
require_once("./cron_job.php");
exit();
}
ob_start(); // will be closed in backup function
register_shutdown_function('cronjob');
SOLUTION 2 (with javascript):
1) create a file with your cronjobs inside
2) put this javascript code in your html:
<script type="text/javascript">
// cron job
$.get('./cron_job.php');
</script>
these methods won’t affect user request time 😉
thanks guys
You can have the PHP script that you call simulate the end of the connection. Meaning it can send the user some arbitrary output and then force the user’s connection to close but still keep running server side.
See the following article: http://php.net/manual/en/features.connection-handling.php
There are some examples in the comments.