To ensure a PHP script automatically restarts if it stops or is closed, you can use several methods to manage the process. A common approach is to use monitoring tools like Supervisor or to write a bash script that automatically restarts the script as needed.

1. Using Supervisor

Supervisor is a process management tool that allows you to run processes as daemons and automatically restart them if they stop.

Installing Supervisor:

On Ubuntu/Debian operating systems, you can install Supervisor using the command:

/etc/supervisor/conf.d/

Add the following content to the file:

auto_restart.sh
  • pgrep -f “php /path/to/your/script.php”: Checks if the script is running.
  • nohup php /path/to/your/script.php: If the script is not running, it restarts it using nohup.
  • sleep 10: Waits 10 seconds before checking again.

Running the Bash Script:

To run this bash script:

/etc/systemd/system/php_script.service

Add the following content:

[Unit]
Description=PHP Script Service
After=network.target

[Service]
ExecStart=/usr/bin/php /path/to/your/script.php
Restart=always
User=www-data
StandardOutput=file:/var/log/php_script.log
StandardError=file:/var/log/php_script.err.log

[Install]
WantedBy=multi-user.target
  • ExecStart: Path to your PHP and script.
  • Restart=always: Automatically restarts if the script stops.
  • User=www-data: Runs the script under the www-data account (or the user you prefer).
  • StandardOutput and StandardError: Log stdout and stderr.

Starting the Service:

After creating the service file, activate and start the service:

sudo systemctl daemon-reload
sudo systemctl enable php_script
sudo systemctl start php_script

Your script will now be managed by systemd and will automatically restart if it stops.

Stopping and Disabling at the Same Time

If you want to stop and disable the automatic start immediately, you can use the following commands:

sudo systemctl stop php_script
sudo systemctl disable php_script

Checking the Service Status

This command will tell you whether the service is running, has stopped, or has been disabled.

sudo systemctl status php_script

Conclusion

Supervisor is an easy and powerful way to manage processes like PHP scripts and automatically restart them if they stop. A bash script is a simple and straightforward method that doesn’t require additional tool installation but may not be as efficient as other solutions. systemd is the standard way to manage processes and services on modern Linux systems.

Keeping a PHP script running indefinitely and automatically restarting it in case of failure is crucial for systems that require high reliability. By using tools like nohup, Supervisor, or systemd, you can easily manage these processes, ensuring they remain active without interruption. Depending on your needs and system configuration, each method has its advantages, allowing you to maintain your PHP scripts effectively. Choose the solution that best fits your infrastructure to ensure long-term performance and stability.