Menu Close

Create a PHP deamon

PHP Logo

Suppose you have a PHP script that you want to run automatically as a service. In Linux, we call that a deamon. You can create a cron job for that, but that is more a scheduled task and not something that runs constantly.

To create a deamon, executing a PHP script constantly, use the following procedure:

Go to the location where the deamon files are stored

cd /etc/init.d

Create a new deamon file and paste your running php code. Make sure you have an infinite loop in the script so it will not exit the script.

nano /etc/init.d/mydeamon

Set execution rights on the new script:

chmod +x /etc/init.d/mydeamon

Register the new deamon

update-rc.d mydeamon defaults 98 02

As soon as you change the contents of the deamon file you need to let the system know of the change by reloading all the deamons:

systemctl daemon-reload

Remove your deamon:

update-rc.d mydeamon remove

Start your deamon:

service mydeamon start

Stop your deamon:

service mydeamon stop

Restart and reloading your deamon:

service mydeamon reload

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *