How to Create PHP Cron Jobs

This article provides an extended look at how to create PHP cron jobs in Linux.

Creating PHP Cron Jobs on a Linux Server

Cron is the standard Linux scheduler used to run scripts automatically at defined intervals. PHP cron jobs are commonly used for automation tasks such as backups, billing runs, log cleanup, monitoring scripts, email campaigns, and application maintenance.

This guide explains how to create, configure, and customize PHP cron jobs using command-line flags and parameters.

  1. Understanding Cron

    Cron is a background daemon that executes commands based on a defined schedule stored in a crontab file.

    A cron schedule has five time fields followed by the command to execute.

    * * * * * command_to_run
    │ │ │ │ │
    │ │ │ │ └── Day of week (0–7) (Sun = 0 or 7)
    │ │ │ └──── Month (1–12)
    │ │ └────── Day of month (1–31)
    │ └──────── Hour (0–23)
    └────────── Minute (0–59)
    

    Example:

    0 2 * * * /usr/bin/php /scripts/backup.php
    

    This runs backup.php every day at 2:00 AM.

  2. Verify PHP CLI is Installed

    Cron jobs should use the PHP CLI binary, not the web version.

    Check the PHP CLI path:

    which php
    

    Typical results:

    /usr/bin/php
    

    Check version:

    php -v
    

    Example output:

    PHP 8.2.10 (cli)
    
  3. Create a Basic PHP Cron Script

    Example script:

    nano /scripts/test-cron.php
    

    Example code:

    <?php
    
    $date = date("Y-m-d H:i:s");
    
    file_put_contents(
        "/tmp/cron_test.log",
        "Cron executed at $date\n",
        FILE_APPEND
    );
    
    ?>

    Test it manually:

    php /scripts/test-cron.php
    

    Check output:

    cat /tmp/cron_test.log
    
  4. Editing the Crontab

    Open the crontab editor:

    crontab -e
    

    Add a job:

    */5 * * * * /usr/bin/php /scripts/test-cron.php
    

    This runs every 5 minutes.

    Save and exit.

    Verify cron jobs:

    crontab -l
    
    Tip! Customize the crontab editor:
    EDITOR=nano crontab -e
  5. Using Full Paths (Very Important)

    Cron runs with a minimal environment, so always use absolute paths.

    Incorrect:

    php test-cron.php
    

    Correct:

    /usr/bin/php /scripts/test-cron.php
    
  6. Redirecting Output and Errors

    Cron jobs generate output which may be emailed or logged.

    Redirect output:

    */5 * * * * /usr/bin/php /scripts/test.php >> /var/log/php-cron.log
    

    Redirect errors:

    */5 * * * * /usr/bin/php /scripts/test.php >> /var/log/php-cron.log 2>&1
    

    Explanation:

    >> append output
    2>&1 send errors to same log
    
  7. PHP CLI Flags for Cron Jobs

    PHP CLI supports flags that customize execution.

    Common flags include:

    Flag Purpose
    -c Specify custom php.ini
    -d Override configuration values
    -q Suppress HTTP headers
    -f Execute PHP script file
    -r Run PHP code directly
    -m Show loaded modules
    -n Ignore php.ini
  8. Using the -c Flag (Custom php.ini)

    The -c option specifies a custom configuration file.

    Example:

    /usr/bin/php -c /opt/custom-php.ini /scripts/worker.php
    

    Use case:

    Different memory limits for cron tasks.

    Example custom config:

    memory_limit = 1024M
    max_execution_time = 0
    

    Cron entry:

    */10 * * * * /usr/bin/php -c /opt/php-cron.ini /scripts/heavy-task.php
    
  9. Using the -d Flag (Override Settings)

    The -d flag temporarily overrides PHP settings.

    Example:

    php -d memory_limit=512M script.php
    

    Cron example:

    0 * * * * /usr/bin/php -d memory_limit=1024M /scripts/report.php
    

    Multiple overrides:

    php -d memory_limit=512M -d max_execution_time=0 script.php
    

    Cron example:

    0 1 * * * /usr/bin/php -d memory_limit=1G -d max_execution_time=0 /scripts/import.php
    
  10. Using the -q Flag

    The -q option suppresses HTTP headers.

    Useful for older PHP scripts designed for CGI.

    Example:

    php -q script.php
    

    Cron example:

    */15 * * * * /usr/bin/php -q /scripts/legacy-script.php
    
  11. Using the -f Flag

    The -f flag explicitly specifies a script file.

    Example:

    php -f /scripts/job.php
    

    Cron example:

    */10 * * * * /usr/bin/php -f /scripts/cache-cleaner.php
    
  12. Passing Arguments to PHP Scripts

    PHP scripts can accept parameters.

    Example script:

    nano /scripts/backup.php
    

    Example code:

    <?php
    
    $type = $argv[1] ?? "full";
    
    echo "Running $type backup\n";
    
    ?>
    

    Run manually:

    php backup.php full
    

    Cron example:

    0 3 * * * /usr/bin/php /scripts/backup.php full
    
  13. Combining Flags and Arguments

    Example advanced cron job:

    */30 * * * * /usr/bin/php -d memory_limit=512M -d max_execution_time=0 /scripts/process-queue.php --limit=100
    

    Explanation:

    -d memory_limit=512M
    -d max_execution_time=0
    --limit=100 passed to script
    
  14. Running PHP Inline with -r

    You can execute PHP code directly.

    Example:

    php -r 'echo date("Y-m-d H:i:s");'
    

    Cron example:

    */10 * * * * /usr/bin/php -r 'file_put_contents("/tmp/time.log", date("c").PHP_EOL, FILE_APPEND);'
    
  15. Running as a Specific User

    Cron jobs should run under the correct user.

    Edit the user's crontab:

    crontab -e
    

    System-wide cron:

    /etc/crontab
    

    Example:

    0 1 * * * www-data /usr/bin/php /var/www/scripts/cleanup.php
    
  16. Locking Cron Jobs (Prevent Overlap)

    Prevent jobs from running simultaneously.

    Example using flock:

    */5 * * * * flock -n /tmp/job.lock /usr/bin/php /scripts/job.php
    
  17. Monitoring Cron Jobs

    Check cron logs:

    grep CRON /var/log/syslog
    

    On RHEL / AlmaLinux:

    grep CRON /var/log/cron
    
  18. Example Production Cron Jobs

    • Queue Worker

      * * * * * /usr/bin/php /var/www/app/artisan queue:work
      
    • Database Cleanup

      0 4 * * * /usr/bin/php -d memory_limit=512M /scripts/db-clean.php
      
    • Cache Purge

      */30 * * * * /usr/bin/php /scripts/cache-purge.php >> /var/log/cache.log 2>&1
      
    • Backup Job

      0 3 * * * /usr/bin/php -d memory_limit=2G /scripts/backup.php full >> /var/log/backup.log 2>&1
      
  19. Security Best Practices

    • Use CLI PHP

      Always use:

      /usr/bin/php
      

      not:

      /usr/local/bin/php-cgi
      
    • Restrict Permissions

      chmod 700 /scripts
      
    • Avoid Running as Root

      Use a dedicated user:

      www-data
      deploy
      automation
      
    • Log Everything

      >> /var/log/script.log 2>&1
      
  20. Example Enterprise PHP Cron Stack

    Example automation environment:

    /opt/cron/
     ├── workers
     │   ├── queue.php
     │   └── billing.php
     ├── logs
     │   └── cron.log
     └── locks
    

    Example cron entries:

    */1 * * * * flock -n /opt/cron/locks/queue.lock /usr/bin/php /opt/cron/workers/queue.php >> /opt/cron/logs/cron.log 2>&1
    
    0 * * * * /usr/bin/php -d memory_limit=512M /opt/cron/workers/billing.php >> /opt/cron/logs/cron.log 2>&1
    
  21. Testing Cron Jobs Safely

    Test with:

    run-parts --test /etc/cron.hourly
    

    Or run manually:

    sudo -u www-data /usr/bin/php /scripts/test.php
    
  22. Troubleshooting

    • Cron not running

      Check service:

      systemctl status crond
      

      Start if needed:

      systemctl enable crond
      systemctl start crond
      
    • PATH problems

      Add PATH in crontab:

      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      
    • Permission errors

      Check user permissions:

      ls -l script.php
      
  23. Example WHMCS Cron Job

    Since many hosting platforms (like WHMCS) rely heavily on PHP cron automation:

    */5 * * * * /opt/cpanel/ea-php84/root/usr/bin/php -c /opt/cpanel/ea-php84/root/etc/php.ini -d date.timezone='America/Chicago' -q /path/to/your/cron.php >> /path/to/your/logfile.log 2>&1
    

    The example calls the specific PHP version binary (/opt/cpanel/ea-php84/root/usr/bin/php), specifies custom php.ini (-c /opt/cpanel/ea-php84/root/etc/php.ini), declares custom timezone (-d date.timezone='America/Chicago'), suppressing HTTP headers (-q), and logs the results to a log file (>> /path/to/your/logfile.log 2>&1).

Conclusion

You now know how to create PHP cron jobs in Linux. Feel free to bookmark this page for reference.

  • php, cron job, crontab
  • 2 Users Found This Useful
Was this answer helpful?

Related Articles

Video: Create Email Address in cPanel

This video tutorial demonstrates how to create an email address using cPanel Shared Hosting...

How to Create Sudo User on CentOS

This article provides step-by-step setup guide for adding Sudo user to CentOS system. The sudo...

How to Use Sudo

This article provides a guide to using a Sudo user on CentOS server. From the command line,...

Set Server Time Zones with Timedatectl

This article provides a guide to setting the server time and server time zone settings using...

htaccess RewriteRule Examples

Here are some useful mod_rewrite RewriteRule redirect examples that you can use in your .htaccess...