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:

sudo apt-get install supervisor

Configuring Supervisor for the PHP Script:

After installing Supervisor, you need to create a configuration file for your PHP script. Typically, the configuration file is located in the /etc/supervisor/conf.d/ directory.

For example, create a configuration file named script.conf:

sudo nano /etc/supervisor/conf.d/script.conf

Add the following content to the file:

[program:php_script]
command=php /path/to/your/script.php
autostart=true
autorestart=true
stderr_logfile=/var/log/php_script.err.log
stdout_logfile=/var/log/php_script.out.log
  • command: Path to the PHP script you want to run.
  • autostart=true: Automatically starts the script when Supervisor starts.
  • autorestart=true: Automatically restarts the script if it stops.
  • stderr_logfile and stdout_logfile: Paths to the log files for recording errors and script output.

Starting Supervisor and Monitoring the Script:

After configuring, restart Supervisor to apply the changes:

sudo supervisorctl reread
sudo supervisorctl update

Now, your PHP script will be monitored and automatically restarted if it stops.

2. Using a Bash Script and nohup

If you prefer not to install a tool like Supervisor, you can use a bash script to check and restart the PHP script if it stops.

Example of an Auto-Restart Bash Script:

Create a bash script file (for example: auto_restart.sh):

#!/bin/bash

while true
do
    # Check if the script is running
    if ! pgrep -f "php /path/to/your/script.php" > /dev/null
    then
        echo "Script is not running. Starting it..."
        nohup php /path/to/your/script.php > /path/to/your/logfile.log 2>&1 &
    else
        echo "Script is already running."
    fi
    # Check again every 10 seconds
    sleep 10
done
  • 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:

nohup bash auto_restart.sh > /dev/null 2>&1 &

This bash script will run indefinitely and check every 10 seconds whether the PHP script is active. If it’s not, it will automatically restart it.

3. Using systemd (on Modern Linux Systems)

systemd is the init system on most modern Linux distributions and allows you to create services that can automatically restart.

Creating a Service File:

Create a service file for your PHP script, for example: /etc/systemd/system/php_script.service.

sudo nano /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.