在Php / Apache / Ubuntu上运行长进程

I'm trying to run long process on Php/Apache/Ubuntu (AWS)

This is a simple process that builds a cache during the night.

The process can run for a few hours, and is initiate by crontab accessing a special url with curl.

Sometimes the process stops at a random with no error, I suspect that it is killed by the apache, although I set

    @set_time_limit(0);
    @ini_set('max_execution_time', -1);

Is it a known issue with Php/Apache/Ubuntu?

Is there a way to solve it?

Currently, my solution is to run the process every 5 minutes, and store the state on the disk, and continue from where it stopped.

But I would like to know more about this issue and if there is a better way to tackle it?

NOTE: The process stops randomly or doesn't stop at all - the longer the process (i.e. bigger cache) the chance it will stop is higher

One possible reason is that the client disconnects (e.g. after a timeout): PHP stops the request processing by default in this case. To prevent this, you can use ignore_user_abort:

ignore_user_abort(true);

Also note that the set_time_limit call may actually fail (e.g. on a restricted environment) — so it might make sense to remove the error suppression (@) or explicitly check whether set_time_limit(0) returned true.