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=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
- 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
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:
- Disable WP-Cron in
wp-config.php: - Add a real cronjob:
define('DISABLE_WP_CRON', true);
*/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:
- Check the path: Make sure you are using full paths in your commands.
- Check permissions: Ensure that the script is executable.
- 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/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
- 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.
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)
Related articles
- Using the DNS Zone Editor in DirectAdmin
- Managing disk space and quotas in DirectAdmin
- Setting up email forwarding in DirectAdmin
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.
0 van 0 vonden dit nuttig