Setting Up Cronjobs in DirectAdmin: Automate Tasks
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
- Log in to DirectAdmin
- Go to Cron Jobs in the menu
- You will see an overview of existing cronjobs
Step 2: Add a new cronjob
- Click on Add Cron Job or Add Cronjob
- Fill in the timing (see below)
- Enter the command
- 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 and 7 = Sunday) |
Common schedules
Here are some commonly used schedules for cronjobs:
- Every minute:
* * * * * - Every hour:
0* * * * - Every 15 minutes:
*/15 * * * * - Every day at 3:00:
0 3 * * * - Every Monday at 9:00:
0 9* * 1 - Every weekday at 7:30:
30 7* * 1-5 - 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/example.nl/public_html/wp-cron.php
Run PHP script
/usr/local/bin/php /home/user/domains/example.nl/public_html/scripts/backup.php
Database backup
/usr/bin/mysqldump -u user -p'your_password' database > /home/user/backups/db-$(date +\%Y\%m\%d).sql
Fetch URL (webhook)
/usr/bin/wget -q -O /dev/null https://example.nl/cron-endpoint
Clear cache
rm -rf /home/user/domains/example.nl/public_html/cache/*
Managing cronjobs
Edit
- Go to Cron Jobs
- Click on the cronjob
- Change timing or command
- Click on Save
Disable
- Add
#at the beginning of the command - The cronjob will be skipped but not deleted
Delete
- Go to Cron Jobs
- Select the cronjob
- 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
>> file appends the output to a log file, > /dev/null discards it and 2>&1 sends error messages to the same place. Note: in DirectAdmin you enter the time in the five time fields; in the Command field you only enter the part starting with /usr/local/bin/php. Check the log file after the scheduled run time to see whether everything went well.
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
By default WordPress uses WP-Cron (wp-cron.php), which only runs scheduled tasks when someone visits your website. On low-traffic sites this is unreliable, on busy sites it adds unnecessary load. Replace it with a real cronjob:
- Disable WP-Cron in
wp-config.php: - Add a real cronjob that runs every 15 minutes (Minute
*/15, other time fields*) with this command:
define('DISABLE_WP_CRON', true);
/usr/local/bin/php /home/user/domains/example.nl/public_html/wp-cron.php > /dev/null 2>&1
Common issues
Cronjob not working
If your cronjob is not working, here are some steps you can follow to troubleshoot the issue:
- Check the path: Make sure you are using full paths in your commands.
- Check permissions: If you call a PHP script via
/usr/local/bin/php, read permission is enough (644). Only a shell script that you execute directly needs 755. - Check output: Redirect output to a log file to view any error messages.
- 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/php81/bin/php /path/to/script.php
The path /usr/local/bin/php uses the default PHP version of the server. Additional PHP versions are located in /usr/local/phpXX/bin/php on DirectAdmin, for example /usr/local/php81/bin/php for PHP 8.1. Use the same version as your website.
Command not found
Always use full paths (e.g., /usr/bin/wget instead of wget). This prevents "command not found" errors.
Tips
- Test manually first: Run the command via SSH before creating the cronjob to ensure it works.
- Use logging: Redirect output to a log file to easily diagnose issues.
- Not too frequent: Avoid every-minute cronjobs unless absolutely necessary. This can unnecessarily burden server resources.
- Test backup scripts: Regularly check that your backups are working and that you can restore them.
- Use full paths: This prevents common errors and makes your cronjobs more reliable.
- Spread heavy tasks: Schedule heavy cronjobs at different times so they do not run simultaneously and put unnecessary load on the server.
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.
Looking for WordPress hosting? See what we offer.
0 of 0 found this helpful