Linux / UNIX Setup and Run PHP Script As A Cron Job

Linux / UNIX Setup and Run PHP Script As A Cron Job

To set up and run a PHP script as a cron job on a Linux or Unix system, you will need to follow these steps:

  1. Open the crontab file for the user that will run the script. You can do this by running the following command:
crontab -e
Sou‮‬rce:www.lautturi.com

This will open the crontab file in your default text editor.

  1. Add a new line to the crontab file to specify when and how often you want the script to be executed. The general syntax for a cron job is:
MINUTE HOUR DOM MONTH DOW COMMAND
  • MINUTE: The minute of the hour at which the command will be executed (0-59).
  • HOUR: The hour of the day at which the command will be executed (0-23).
  • DOM: The day of the month on which the command will be executed (1-31).
  • MONTH: The month of the year in which the command will be executed (1-12).
  • DOW: The day of the week on which the command will be executed (0-6, where 0 is Sunday).

For example, to run the script every hour on the hour, you would specify 0 * * * *.

  1. After the schedule, add the command to run the PHP script. The general syntax is:
php -f /path/to/script.php

Replace /path/to/script.php with the path to your PHP script on the system.

  1. Save and close the crontab file. The cron daemon will automatically pick up the changes and start running the script according to the specified schedule.
Created Time:2017-10-29 22:08:55  Author:lautturi