A long-running PHP worker needs supervision. Starting a worker manually in a terminal is not a production process-management strategy: it can stop when a session closes, fail silently or multiply into duplicate processes after an unclear restart.
Define a dedicated service
Create a systemd unit for the worker command and run it as a restricted operating-system user. Set the working directory explicitly and load only the environment configuration required by the process. Avoid running application workers as root.
Choose restart behavior carefully
A restart policy can recover from unexpected exits, but an aggressive restart loop can hide a persistent configuration error. Use a sensible restart delay and inspect service status and journal logs during deployment. Make the worker exit with a meaningful status when it cannot operate safely.
Handle shutdown gracefully
Workers should stop accepting new jobs when they receive a termination signal, finish or safely release the current job, and exit within the service's stop timeout. Ensure job visibility timeouts and application-level locks do not leave work permanently stuck after termination.
Apply resource controls
Set appropriate memory and file-descriptor limits, and monitor CPU and memory usage. A worker that gradually grows in memory may need a controlled recycle policy. Do not use systemd restart settings as a substitute for fixing a memory leak.
Checklist
- Run workers as a dedicated unprivileged user.
- Configure working directory and environment explicitly.
- Use bounded restart behavior.
- Implement graceful shutdown and job release.
- Monitor service health and resource consumption.