Schedule a task with a cron job

Write a small PHP task, schedule it in DirectAdmin, log its output and confirm it runs on time.

TutorialIntermediate20 min to completeLast updated
On this page
  1. What you’ll need
  2. Step 1: Create a folder outside public_html
  3. Step 2: Write the task
  4. Step 3: Open Cronjobs
  5. Step 4: Set the schedule
  6. Step 5: Enter the command
  7. Step 6: Wait and check
  8. Step 7: Replace with your real task
  9. Step 8: Remove the test
  10. Check it worked
  11. If something goes wrong
Level
Intermediate
Time
About 20 min
Steps
8
You’ll finish with
A working result, checked

8 steps

What you’ll need

  • DirectAdmin access
  • Your DirectAdmin username (for paths)

You’ll create a task that writes a timestamp to a log file every five minutes. Once that works, swap in your real task.

Step 1: Create a folder outside public_html

In System Info & Files › File Manager, create /home/USERNAME/tasks and /home/USERNAME/logs.

Step 2: Write the task

Create /home/USERNAME/tasks/heartbeat.php:

<?php
$line = date('c') . " heartbeat ok\n";
file_put_contents(__DIR__ . '/../logs/heartbeat.log', $line, FILE_APPEND);
echo $line;

Step 3: Open Cronjobs

Open Advanced Features › Cronjobs.

Step 4: Set the schedule

Minute */5, and * for hour, day of month, month and day of week. Cron schedule syntax explained (with examples)

Step 5: Enter the command

/usr/local/bin/php /home/USERNAME/tasks/heartbeat.php >> /home/USERNAME/logs/cron.log 2>&1

Replace USERNAME in both places. Create the job.

Step 6: Wait and check

After ten minutes, open logs/heartbeat.log. You should see two timestamped lines.

Step 7: Replace with your real task

Edit the command to run your real script, keeping the log redirect until you’re confident it works. Running PHP scripts from cron: paths, output and common mistakes

Step 8: Remove the test

Delete the heartbeat job and files.

Check it worked

heartbeat.log gained a new line roughly every five minutes, and cron.log contains no errors.

If something goes wrong

What happensWhat to do
No log lines at allCheck the schedule saved, and the full paths. Cron job not running
cron.log shows ‘No such file’Wrong path or USERNAME not replaced.
cron.log shows ‘command not found’PHP binary path differs. Running PHP scripts from cron: paths, output and common mistakes

New to Traxio? The PHP hosting page lists the PHP versions, database and tools every account gets.

Where this fits: DirectAdmin hosting explains the control panel every Traxio account uses, with a tour and tutorials.

Spotted something out of date or unclear? Tell us and we’ll fix it.

Keep going