Web Development Tutorials

Cron Jobs Explained: Schedule PHP Scripts on Hostinger and cPanel

Daily backups, reminder emails and cleanup tasks shouldn't depend on someone clicking a button. Here's how cron jobs work and how to set them up for PHP.

In this article · 7 sections
  1. Cron syntax
  2. Common schedules
  3. Example: daily reminder emails
  4. Setting it up
  5. Good practices
  6. Ideas for business websites
  7. FAQs
  8. My cron job doesn't run. Why?
  9. Can I trigger a URL instead?

Cron jobs run commands automatically on a schedule: send booking reminders every morning, clean old sessions every night, generate a sitemap weekly, back up the database daily. Most hosting panels, including Hostinger hPanel and cPanel, let you set them up without server access.

Cron syntax

* * * * *  command
│ │ │ │ │
│ │ │ │ └── day of week (0–7, Sunday = 0 or 7)
│ │ │ └──── month (1–12)
│ │ └────── day of month (1–31)
│ └──────── hour (0–23)
└────────── minute (0–59)

Common schedules

Schedule Expression
Every 15 minutes */15 * * * *
Every day at 2:30 AM 30 2 * * *
Every Monday at 9 AM 0 9 * * 1
First day of each month 0 6 1 * *

Server time may be UTC rather than IST (IST = UTC + 5:30). Check your host's timezone before choosing times.

Example: daily reminder emails

cron/reminders.php:

<?php
if (PHP_SAPI !== 'cli') { http_response_code(403); exit; }   // run only from cron
require __DIR__ . '/../connection.php';

$res = $conn->query("SELECT id, name, phone FROM bookings
                     WHERE travel_date = CURDATE() + INTERVAL 1 DAY AND status = 'confirmed'");
while ($b = $res->fetch_assoc()) {
    // send reminder by email/SMS/WhatsApp API…
}
echo date('c') . " reminders done\n";

Setting it up

Hostinger hPanel: Advanced → Cron Jobs → choose "PHP" or "Custom", enter the script path and schedule.

cPanel: Advanced → Cron Jobs → add a command like:

/usr/local/bin/php /home/USERNAME/public_html/cron/reminders.php >> /home/USERNAME/cron.log 2>&1

The PHP path differs between hosts; your panel usually shows it.

Good practices

  • Keep cron scripts outside the public folder, or block web access (as in the PHP_SAPI check above)
  • Log output to a file so you can see whether it ran
  • Prevent overlap for long tasks using a lock file:
<?php
$lock = fopen(__DIR__ . '/reminders.lock', 'c');
if (!flock($lock, LOCK_EX | LOCK_NB)) exit("Already running\n");
  • Don't schedule heavy jobs every minute on shared hosting
  • Send yourself an alert if an important job fails

Ideas for business websites

  • Daily database backup to cloud storage. See website backups
  • Booking and payment reminders
  • Delete old uploads and logs. See DPDP basics
  • Regenerate sitemaps and cache

FAQs

My cron job doesn't run. Why?

Wrong PHP path, wrong file path (use absolute paths), or a PHP error. Check the log file.

Can I trigger a URL instead?

Yes, with curl or wget, but protect that URL with a secret key.

Was this article helpful?

Your answer helps us improve our guides

Share this article

Joboo Web

Web design, SEO & digital marketing team · Ludhiana, Punjab

Joboo Web has helped Indian businesses grow online since 2019 — building fast websites and running SEO, Google Ads and social campaigns for clients in 80+ cities. About us →

Keep reading

💬