arrow_back
Back

Running PHP scripts as daemons

Andrew Dorokhov Andrew Dorokhov schedule 1 min read
menu_book Table of Contents

You can start a PHP script from the command line and keep it running as a background process:

nohup php myscript.php &
  • & puts the process in the background so it keeps running after you close the shell.
  • nohup prevents the process from being killed when the parent shell session ends (it ignores the SIGHUP signal).

Stopping it

A plain kill <pid> stops it — no special tooling required. For a script that needs to shut down cleanly (flush buffers, close connections), have it listen for SIGTERM with PCNTL and exit gracefully instead of relying on SIGKILL.

When this is enough

This approach is simple on purpose. For anything beyond a single long-running background script — multiple workers, automatic restarts, log rotation — a process manager (such as Supervisor or systemd) is usually a better fit than rolling your own daemon management.

code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward