...
How to create php cron jobs on linux server
Learn how to create php cron jobs on linux server!

This guide demonstrates how to create PHP cron jobs on Linux server.

What are PHP Cron Jobs?

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.

READ ALSO: cPanel Shared Hosting Server Specs

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

How to Create PHP Cron Jobs on Linux Server

To create PHP cron jobs on Linux server, follow the steps below:

  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:

     

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

     

    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 * * * * /usr/bin/php -q /home/whmcs/crons/cron.php
    

Conclusion

You now know how to create PHP cron jobs on Linux server.


✅ Easy Website Management with cPanel Support

Running a business is hard enough — managing your website shouldn’t be. That’s why we include full cPanel support with every hosting plan:

  • Simple Dashboard – No tech skills needed. Easily manage your website, emails, and more from one place.
  • Quick App Installs – Launch WordPress, shopping carts, or other tools with just one click.
  • Professional Email – Create business email addresses (like you@yourbusiness.com) in minutes.
  • Reliable Backups – Keep your website safe with easy-to-use backup and restore options.
  • Secure & Protected – Manage your site’s security and SSL certificates with built-in tools.
  • Real Help, AnytimeOur expert support team is available 24/7 for anything you need.

💬 What Our Customers Say

“I have had nothing but good experiences with Rad Web Hosting. The staff is there to help you to make sure that you stay online and I haven't had any downtime with my server in the time I have been with Rad Web Hosting and I have had my server for over two years.”

— Janice L., Owner of RJGM

🏆 Trusted by Small Business Owners Nationwide

Trust badges: 99. 9% uptime | 24/7 support | secure hosting | money-back guarantee

🚀 Get Started Today

Take the stress out of website management. With cPanel support and expert help just a click away, you can focus on what matters most — growing your business.

Choose Your Plan Now

Avatar of editorial staff

Editorial Staff

Rad Web Hosting is a leading provider of web hosting, Cloud VPS, and Dedicated Servers in Dallas, TX.
lg