Ubuntu Server – Run PHP script on startup

There are several ways to run a PHP script on Ubuntu server startup. Here are a few options:

  1. Using the crontab command: You can use the crontab command to schedule the script to run at startup. To do this, open a terminal and type crontab -e. This will open the crontab file in a text editor. Add the following line at the end of the file, replacing “/path/to/script.php” with the actual path to your PHP script:
@reboot /usr/bin/php /path/to/script.php

This will run the script every time the server is restarted.

  1. Using a startup script: You can create a startup script that runs your PHP script at boot time. To do this, create a new file in the /etc/init.d directory, such as myscript.sh. Add the following lines to the file, replacing “/path/to/script.php” with the actual path to your PHP script:
#!/bin/bash
/usr/bin/php /path/to/script.php &

Make the script executable by running chmod +x /etc/init.d/myscript.sh. Then, use the update-rc.d command to configure the script to run at startup:

sudo update-rc.d myscript.sh defaults
  1. Using the systemd service: You can also create a systemd service to run your PHP script at startup. To do this, create a new file in the /etc/systemd/system directory, such as myscript.service. Add the following lines to the file, replacing “/path/to/script.php” with the actual path to your PHP script:
[Unit]
Description=My Script

[Service]
Type=simple
ExecStart=/usr/bin/php /path/to/script.php

[Install]
WantedBy=multi-user.target

Then, use the systemctl command to enable and start the service:

Copy codesudo systemctl enable myscript.service
sudo systemctl start myscript.service

These are just a few examples of how you can run a PHP script on Ubuntu server startup. You can choose the method that best fits your needs and preferences.

This is auto-generated from chatGPT.

Leave a Comment

Your email address will not be published.

Free Web Hosting