A cronjob automatically executes tasks at scheduled times. In this guide, we explain how to set up cronjobs in DirectAdmin. This can help you save time and ensure that important tasks are performed regularly without needing manual oversight.

What is a cronjob?

A cronjob (or cron task) is a scheduled task that is automatically executed at specific times. This can range from daily database backups to generating weekly reports. Here are some examples of common cronjobs:

  • Daily database backup
  • Clear cache every hour
  • Generate weekly report
  • Run scripts periodically

Cronjobs are particularly useful for web developers and system administrators, as they automate repetitive tasks and reduce the likelihood of human errors.

Creating a cronjob

Step 1: Navigate to Cron Jobs

  1. Log in to DirectAdmin
  2. Go to Cron Jobs in the menu
  3. You will see an overview of existing cronjobs

Step 2: Add a new cronjob

  1. Click on Add Cron Job or Add Cronjob
  2. Fill in the timing (see below)
  3. Enter the command
  4. Click on Add or Add

Configuring timing

A cronjob uses 5 time fields:

Field Range Meaning
Minute 0-59 At which minute
Hour 0-23 At which hour
Day 1-31 On which day of the month
Month 1-12 In which month
Weekday 0-7 On which day (0=sunday)

Common schedules

Here are some commonly used schedules for cronjobs:

  • Every minute:
    * * * * *
  • Every hour:
    0* * * *
  • Every day at 3:00:
    0 3* * *
  • Every Monday at 9:00:
    0 9 * * 1
  • First day of the month:
    0 0 1 * *

Command examples

Here are some examples of commands you can use in your cronjobs:

Run WordPress cron

/usr/local/bin/php /home/user/domains/site.nl/public_html/wp-cron.php

Run PHP script

/usr/local/bin/php /home/user/domains/site.nl/public_html/scripts/backup.php

Database backup

/usr/bin/mysqldump -u user -p'wachtwoord' database > /home/user/backups/db-$(date +\%Y\%m\%d).sql

Fetch URL (webhook)

/usr/bin/wget -q -O /dev/null https://site.nl/cron-endpoint

Clear cache

rm -rf /home/user/domains/site.nl/public_html/cache/*

Managing cronjobs

Edit

  1. Go to Cron Jobs
  2. Click on the cronjob
  3. Change timing or command
  4. Click on Save

Disable

  1. Add # at the beginning of the command
  2. The cronjob will be skipped but not deleted

Delete

  1. Go to Cron Jobs
  2. Select the cronjob
  3. Click on Delete

Output and logging

Output to file

0 3* * * /usr/local/bin/php /path/to/script.php >> /home/user/logs/cron.log 2>&1

Ignore output

0 3 * * * /usr/local/bin/php /path/to/script.php > /dev/null 2>&1

Email output

DirectAdmin can email cronjob output. Set this up via Cron Jobs settings. This can be useful to stay informed of any errors or issues that occur while executing the cronjob.

WordPress and cronjobs

WordPress uses wp-cron.php for scheduled tasks. For better performance:

  1. Disable WP-Cron in wp-config.php:
  2. define('DISABLE_WP_CRON', true);
  3. Add a real cronjob:
  4. */15 * * * * /usr/local/bin/php /home/user/domains/site.nl/public_html/wp-cron.php

Common issues

Cronjob not working

If your cronjob is not working, here are some steps you can follow to troubleshoot the issue:

  1. Check the path: Make sure you are using full paths in your commands.
  2. Check permissions: Ensure that the script is executable.
  3. Check output: Redirect output to a log file to view any error messages.
  4. Test manually: Run the command via SSH to see if it works.

Wrong PHP version

If you need a specific PHP version, specify it in your cronjob:

/usr/local/bin/ea-php81 /path/to/script.php

Command not found

Always use full paths (e.g., /usr/bin/wget instead of wget). This prevents "command not found" errors.

Tips

  1. Test manually first: Run the command via SSH before creating the cronjob to ensure it works.
  2. Use logging: Redirect output to a log file to easily diagnose issues.
  3. Not too frequent: Avoid every-minute cronjobs unless absolutely necessary. This can unnecessarily burden server resources.
  4. Test backup scripts: Regularly check that your backups are working and that you can restore them.
  5. Use full paths: This prevents common errors and makes your cronjobs more reliable.

Cron syntax tips

The cron syntax can be confusing. Here are some handy tools:

Crontab Guru

Use crontab.guru to verify your cron timing. Enter your timing and you will see immediately when the job runs.

Special characters

  • * = any value
  • , = list of values (e.g., 1,3,5)
  • - = range (e.g., 1-5)
  • / = step values (e.g., */15 = every 15)

Need help?

We are here for you! Are you facing any issues or have questions? Our support team is happy to assist you personally. Send us a message via the ticket system - we usually respond within a few hours and are eager to help.