How to Set Up Cron Jobs in DirectAdmin

Add a cron job in DirectAdmin, get the schedule and PHP binary path right, run WP-Cron and the Laravel scheduler, log output, and fix a job that never runs.

Published
Reading time
5 min

A cron job is a command the server runs for you on a schedule. In DirectAdmin every user has their own crontab, edited from the panel, and the commands run as that user with that user's permissions. Getting one working is mostly a matter of two details: the schedule syntax and the path to the right PHP. This post covers both, the two most common uses, and what to check when a job silently does nothing.

The Cron Jobs page

At User Level, open Advanced Features → Cron Jobs and click Create Cron Job. The form has the five schedule fields, minute, hour, day of month, month, day of week, and a command box. Save and the entry is written to the user's system crontab, which you can confirm from a shell with crontab -l as that user, or as root by reading /var/spool/cron/bob on the RHEL family or /var/spool/cron/crontabs/bob on Debian.

Each field takes a number, * for every, */5 for every five, 1,15 for a list, or 9-17 for a range. The examples that cover almost everything:

*/5 * * * *    every five minutes
0 * * * *      every hour, on the hour
30 2 * * *     02:30 every day
0 3 * * 0      03:00 every Sunday
0 0 1 * *      midnight on the first of the month

Times are the server's timezone, which the administrator sets, so a job scheduled for 02:30 on a server set to UTC runs at 03:30 during British Summer Time. The page also has an email field: any output the command prints is mailed there, which is useful once and a nuisance forever, so read on before you leave it set.

Use the right PHP binary

This is the detail that breaks more cron jobs than everything else combined. CustomBuild installs each PHP version in its own directory, /usr/local/php83/bin/php, /usr/local/php84/bin/php and so on. Plain php on the command line resolves to /usr/local/bin/php, which is a link to the server's primary version, not necessarily the one the domain runs on. A site on 8.4 whose cron runs under 8.1 will fail on syntax it cannot parse or on an extension that is not there.

So always use the full path, and match it to the version chosen for the domain in Domain Setup, which is explained in changing the PHP version in DirectAdmin:

bash
/usr/local/php84/bin/php /home/bob/domains/example.com/public_html/cron.php

Use full paths for the script as well. Cron runs with a minimal environment and no working directory you can rely on, so php cron.php finds nothing.

WP-Cron the right way

WordPress runs its scheduled tasks on page loads by default, which means a quiet site publishes scheduled posts late and a busy site pays for a cron check on every request. Turn that off in wp-config.php:

ini
define('DISABLE_WP_CRON', true);

Then add a real cron job every five minutes:

bash
/usr/local/php84/bin/php /home/bob/domains/example.com/public_html/wp-cron.php

Running the PHP file directly is better than fetching the URL with curl, because it does not depend on the site being reachable over HTTP, does not get blocked by a security plugin, and runs with the same PHP the site uses. If you have several WordPress sites under one user, each needs its own line.

The Laravel scheduler

Laravel wants a single entry every minute and does its own scheduling inside:

bash
* * * * * /usr/local/php84/bin/php /home/bob/domains/app.example.com/artisan schedule:run >> /dev/null 2>&1

The application root is the directory containing artisan. Queue workers are long-running processes rather than cron jobs; on shared hosting the practical substitute is queue:work --stop-when-empty on the same one-minute schedule.

Logging output instead of emailing it

Cron mails whatever the command prints to the address on the Cron Jobs page. For a job that prints something every run, that is 288 emails a day. Redirect the output instead:

bash
/usr/local/php84/bin/php /home/bob/domains/example.com/public_html/cron.php >> /home/bob/cron.log 2>&1

>> appends standard output to the file, 2>&1 sends errors to the same place. The log grows until you truncate it, so keep an eye on it. > /dev/null 2>&1 discards everything, which is fine for a job you trust and terrible for one you are still debugging.

Why a job did not run

Work through these in order.

Run the command by hand first. Log in over SSH as the user, or as root use su - bob -s /bin/bash, paste the exact command from the Cron Jobs page and watch what happens. Most problems reproduce immediately: a wrong path, a missing PHP extension, a permissions error on a file the script writes.

Check the schedule. A job set to 0 0 * * * you added at 09:00 will not run until midnight. */5 in the hour field, rather than the minute field, runs once every five hours.

Check the crontab was written. crontab -l -u bob as root. If the panel entry exists but the crontab is empty, look in /var/log/directadmin/errortaskq.log; the task queue is what writes crontabs and it logs why it could not.

Check crond is running. systemctl status crond (RHEL family) or cron (Debian). The service also appears in Admin Level → Server Manager → Service Monitor. /var/log/cron or journalctl -u crond shows every job cron started, which separates "cron never started it" from "it started and failed".

On CloudLinux, check CageFS. Cron jobs run inside the user's cage. A binary that exists on the server but is not in the CageFS skeleton is invisible to the job; cagefsctl --update as root after installing anything new usually fixes it. The user's /tmp is also private, so a job that writes to /tmp for another process to pick up will not work. A job that dies part way through with no PHP error is usually hitting the LVE memory limit. Details are in CageFS: what it does and how to enable it.

On a VPSPioneer managed VPS with DirectAdmin we can set up the WP-Cron and scheduler jobs above for you and check the log the first morning.

#directadmin#cron#php#automation

Keep reading

More from DirectAdmin

All guides

DirectAdmin

How to Change the PHP Version and Settings in DirectAdmin

Install several PHP versions with CustomBuild, pick one per domain in DirectAdmin, raise memory_limit and upload sizes, and check what is actually in effect.

5 min read →

DirectAdmin

DirectAdmin Backups and Restore: Admin, Reseller and User Level

Schedule DirectAdmin backups in Admin Backup/Transfer, send them to FTP or SFTP, restore a user on another server, copy offsite with rclone and test a restore.

5 min read →

DirectAdmin

How to Manage DNS Records in DirectAdmin

Add and edit A, CNAME, MX and TXT records in DirectAdmin's DNS Management, enable DKIM, set nameservers and glue, use Cloudflare instead, and check propagation.

5 min read →