How to set up a cron job

What is a cron job?

A cron job runs a command automatically on a schedule.

Common uses include:

  • Running application maintenance
  • Sending queued messages
  • Processing scheduled tasks
  • Creating application-generated reports
  • Running Laravel's scheduler
  • Calling a maintenance script

Do not schedule a job more frequently than the application requires.

Understand the schedule

A cron schedule contains five time fields:

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

Examples:

Schedule Meaning
0 * * * * At the start of every hour
*/15 * * * * Every 15 minutes
30 2 * * * Every day at 02:30
0 4 * * 1 Every Monday at 04:00
0 0 1 * * At midnight on the first day of each month

Times are South African time (SAST).

Create a cron job in cPanel

  1. Sign in to cPanel.
  2. Open Advanced.
  3. Select Cron Jobs.
  4. Choose a common schedule or enter the time fields.
  5. Enter the command.
  6. Select Add New Cron Job.

Create a cron job in DirectAdmin

  1. Sign in to DirectAdmin.
  2. Open Advanced Features.
  3. Select Cron Jobs.
  4. Select Create Cron Job.
  5. Enter the schedule and command.
  6. Save the cron job.

Use absolute paths

Cron does not necessarily start in your website folder. Use full paths to scripts and files.

Example:

/usr/local/bin/php /home/USERNAME/public_html/path/to/script.php

For DirectAdmin:

/usr/local/bin/php /home/USERNAME/domains/DOMAIN/public_html/path/to/script.php

Replace the placeholders with your account details.

/usr/local/bin/php runs the PHP version you selected for your account.

Save output to a log

To save both normal output and errors:

/path/to/command >> /home/USERNAME/cron.log 2>&1

Make sure the destination folder exists and is writable.

Review or remove old cron logs periodically so they do not consume unnecessary disk space.

Avoid exposing secrets

Do not place passwords or API keys directly in a cron command where possible.

Use:

  • Environment variables
  • A protected configuration file outside public_html
  • The application's normal configuration system

Test the command first

  1. Add the job with a schedule a few minutes ahead, and send its output to a log as above.
  2. Check the log after it runs.
  3. Fix any errors, then set the real schedule.

SSH is switched off by default on shared hosting. If you want to run commands by hand first, open a support ticket to ask for SSH access.

Did this answer it?