Monday, February 29, 2016

How to run a PHP script with cron on Ubuntu (every 30 minutes)

For one of the customer, I wanted to execute a PHP script every 30 minutes automatically on Ubuntu. This can be achieved by setting up a cron job on Ubuntu. The same idea can be achieved on Windows by using the scheduler; see my previous post: Cron Job Creation for windows server to run PHP Script.

Prerequisites

  • PHP CLI installed (package often named php-cli or provided by the php package on modern Ubuntu). Verify with:
which php
php -v
  • The PHP script you want to run; make sure you use absolute paths in cron.
  • Basic access to the user account that will run the cron job.

Make the script executable (optional)

If you want to run the script directly (instead of calling the php binary), add a shebang to the top of your PHP file and make it executable.

Example shebang (adjust path if which php shows a different location):

#!/usr/bin/php
<?php
// your code

Make the script executable:

chmod +x /path/to/script.php

Open a terminal

On most Ubuntu desktop systems press Alt+Ctrl+T to open a terminal.

Edit the crontab

Run:

crontab -e

This opens your user's crontab in the editor. Add a line to run the script every 30 minutes.

Example: call PHP binary and script with absolute paths

0,30 * * * * /usr/local/bin/php /home/username/public_html/export.php

This example is taken from the original post. Ensure /usr/local/bin/php is the correct PHP CLI path on your system (use which php to confirm). If which php prints /usr/bin/php, use that instead.

Alternative: run the script directly (with shebang)

If your script is executable and has the correct shebang, you can call it directly:

0,30 * * * * /home/username/public_html/export.php

Cron schedule explanation

A cron line has five time fields followed by the command:

minute hour day-of-month month day-of-week command

0,30 * * * * means "at minute 0 and 30 of every hour, every day" (i.e., every 30 minutes).

Recommended improvements for reliability

  • Always use absolute paths for the PHP binary and the script.
  • Cron runs with a minimal environment — do not rely on shell environment variables unless you set them in the crontab.
  • If your script references files or other executables, use absolute paths for those too.
  • Capture output and errors to a log file to help debugging:
0,30 * * * * /usr/bin/php /home/username/public_html/export.php >> /home/username/logs/export.log 2>&1
  • If you need particular environment variables (PATH, HOME, etc.), set them at the top of the crontab:
PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash

Troubleshooting

  • Verify the crontab entry exists:
crontab -l
  • Test the command manually from the same user account:
/usr/bin/php /home/username/public_html/export.php
  • Check cron logs. On many Ubuntu systems:
# syslog (older setups)
grep CRON /var/log/syslog

# systemd-based systems
journalctl -u cron
  • Ensure file permissions: the user running the cron job must have execute/read access to the script and any resources it uses.

Further reading / help

For more information about crontab run:

crontab --help
man crontab

That's it. Happy Coding!!!

No comments: