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.

IntermediateAbout 20 minutes8 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.

Steps

  1. Step 1: Create a folder outside public_html

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

  2. 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;
  3. Step 3: Open Cronjobs

    Open Advanced Features then Cronjobs.

  4. Step 4: Set the schedule

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

  5. 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.

  6. Step 6: Wait and check

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

  7. 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

  8. 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, free for the first 30 days.

Popular

Tip: press / to search from any pageSee all results