Cron Jobs & Scheduled Tasks

How to Set Up a Cron Job

By the StarDomain teamPublished 3 min read
Knowledge base article
Contents (5 sections)

A cron job runs a command on a schedule: for example, a PHP script every night, or WordPress's scheduled tasks every 15 minutes.

Key takeaways

Container Hosting: open the Cron jobs tab, add the job, then press Save. DirectAdmin hosting: use Cron Jobs in DirectAdmin. Always use full paths, and avoid running jobs more often than you need.

1. Container Hosting

  1. Open your container
    in the client area and go to the Cron jobs tab.
  2. Add a job.
    Choose a preset or type a schedule, then enter the command.
  3. Press Save.
    Changes wait until you save, so you can add or remove several jobs at once.

To run a PHP script, call PHP with the full path to the script:

code
php /home/<your user>/public_html/cron.php

A command cannot contain a % character or a line break. More detail is in PHP Settings and Cron Jobs.

2. DirectAdmin hosting

  1. Log in to DirectAdmin
    and open Cron Jobs (under Advanced Features or Extra Features, depending on your skin).
  2. Enter the schedule
    (minute, hour, day of month, month, day of week) and the command.
  3. Click Create
    (or Add).

3. The schedule format

code
* * * * *
| | | | └── day of week (0-7, 0 and 7 = Sunday)
| | | └──── month (1-12)
| | └────── day of month (1-31)
| └──────── hour (0-23)
└────────── minute (0-59)
ScheduleRuns
/15 *Every 15 minutes
0 /6 Every 6 hours
30 2 *Every day at 02:30
0 9 1Every Monday at 09:00
0 0 1 First day of each month at midnight

4. WordPress cron

WordPress runs its scheduled tasks when someone visits the site. On a quiet site they can run late. To run them on time:

  1. Add define('DISABLE_WP_CRON', true); to wp-config.php.
  2. Add a cron job every 15 minutes that runs php /home/<your user>/public_html/wp-cron.php.

5. Good habits

  • Use full paths for PHP, scripts and files.
  • Don't run jobs too often. Every minute is rarely needed and uses your plan's resources.
  • Keep the output. Send it to a log file, for example >> /home/<your user>/logs/cron.log 2>&1, so you can see what happened.
My Container Hosting cron job does not run. What should I check?

Check that you pressed Save, that the command uses full paths and contains no % character, and look in the Logs tab for errors.

How often can a cron job run?

At most once a minute, but choose the longest interval that does the job.

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app
How to Set Up a Cron Job