Reputation: 15221
I'm planning the development of a server written in PHP that can service socket requests. I use a free host (Heliohost) for testing, and it has cPanel. So far the only thing I've been able to think of to have a PHP script always running is to write a cron job that runs a bash script to check ps
to see if the PHP is already running, and if it isn't, start it.
Is there a better way? Perhaps a way for a PHP thread to be started on an HTTP request and continue to run in Apache after the request has been serviced?
Upvotes: 1
Views: 1089
Reputation: 104040
You will almost certainly not have success running persistent processes from Apache. It is designed to prevent that scenario (though if you can get to the fork(2)
system call, it is probably do-able). I wouldn't recommend trying it though.
What would make more sense is if you use a hosting provider that gives you the ability to write your own crontab(5)
specifications and run the PHP interpreter directly. Then you could just add a line to your crontab(5)
like:
@reboot /path/to/php /path/to/script.php
Your script should probably perform the usual daemonization tasks so that cron(8)
isn't stuck waiting for your process to exit.
Upvotes: 2