StarDomain

Common Cron Job Examples for DirectAdmin

Common Cron Job Examples

This guide provides ready-to-use cron job commands for common tasks on DirectAdmin shared hosting. Copy and paste these into your cron job configuration.

WordPress Cron

WordPress has a built-in pseudo-cron that runs on page visits. For better reliability, disable WP-Cron and use a real cron job:

  1. Add this to wp-config.php:
define('DISABLE_WP_CRON', true);
  1. Set up a cron job to run every 5 minutes:
*/5 * * * * /usr/local/bin/php /home/username/domains/yourdomain.com/public_html/wp-cron.php >/dev/null 2>&1

Database Backup

Back up your MySQL database daily at 2 AM:

0 2 * * * /usr/bin/mysqldump -u dbuser -p'dbpassword' dbname > /home/username/backups/db-$(date +\%%Y\%%m\%%d).sql 2>&1

Tip: Store backups outside your public_html directory so they are not accessible via the web.

Clear Cache or Temp Files

Delete files older than 7 days from a temp directory, daily at 3 AM:

0 3 * * * find /home/username/domains/yourdomain.com/public_html/cache -type f -mtime +7 -delete >/dev/null 2>&1

Send Scheduled Emails (PHP Script)

Run a PHP script every hour to process an email queue:

0 * * * * /usr/local/bin/php /home/username/domains/yourdomain.com/public_html/send-emails.php >/dev/null 2>&1

Laravel Task Scheduler

Laravel requires a single cron entry that runs every minute:

* * * * * cd /home/username/domains/yourdomain.com/public_html && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1

Node.js Script

Run a Node.js script daily at midnight:

0 0 * * * /usr/local/bin/node /home/username/domains/yourdomain.com/scripts/cleanup.js >/dev/null 2>&1

Log Rotation

Rotate application logs weekly on Sunday at 1 AM:

0 1 * * 0 mv /home/username/logs/app.log /home/username/logs/app-$(date +\%%Y\%%m\%%d).log && touch /home/username/logs/app.log >/dev/null 2>&1

Checking Cron Output

To debug a cron job, redirect output to a log file:

*/5 * * * * /usr/local/bin/php /home/username/script.php >> /home/username/cron-debug.log 2>&1

Then check the log:

tail -50 /home/username/cron-debug.log

Best Practices

  1. Always use full paths for commands and scripts
  2. Redirect output to prevent email floods
  3. Test your command via SSH first before adding it as a cron job
  4. Avoid running resource-heavy scripts more frequently than necessary
  5. Use lock files to prevent overlapping runs of long-running scripts

Need help? Contact our support team at {{SUPPORT_EMAIL}} or open a ticket at {{SUPPORT_URL}}.