F FreeCronJob
← Blog

How to Automatically Run a PHP Script Daily

How to Automatically Run a PHP Script Daily

Running a PHP script automatically every day is essential for tasks like clearing temp files, sending daily reports, or updating cached data. Cron is the standard scheduler on Unix-like systems, and it works well with PHP. For website owners without root server access, two effective paths exist: direct command-line execution, or a secure HTTP endpoint. Both have advantages, and this article helps you choose the right daily automation method.

Understanding PHP Cron Jobs

A PHP cron job is a scheduled task that runs a PHP script at set intervals. The cron daemon reads crontab files with lines like 0 2 * * * /usr/bin/php /home/user/script.php, meaning run at 2:00 AM daily. The five asterisks represent minute, hour, day, month, weekday. For daily execution, set hour and minute fields and leave the rest as asterisks. Since PHP scripts often behave differently under CLI, test with php -l for syntax and php script.php before scheduling.

Method 1: Direct Command Execution

With shell access, edit your crontab using crontab -e. Use full paths to the PHP binary and your script. Example: 30 3 * * * /usr/bin/php /var/www/html/scripts/daily.php >> /var/log/daily.log 2>&1. This logs output and errors for debugging. Direct execution runs in CLI, so $_SERVER variables aren't available. Use getcwd() or define constants carefully. Also ensure the script has read permission. Cron jobs run with minimal PATH, so always use absolute paths.

Method 2: Secure HTTP Endpoint

If you don't have shell access, or if your script relies on web-server functions like sessions or specific $_SERVER values, a better approach is to expose a special endpoint. Create a PHP script that checks a secret token passed via query string or header. For example: if ($_GET['token'] !== 'your-secret') { http_response_code(403); exit; } Then schedule a cron job that uses curl or wget to fetch the URL: curl -s "https://example.com/cron.php?token=your-secret". To prevent external users from triggering the task, only allow requests from localhost? Actually if cron is on another server, need allow. Better to use a random token and HTTPS. Also consider setting a time limit or using a lock file to prevent overlapping executions.

Choosing the Right Approach

Both methods can achieve reliable daily automation. The table below compares their key aspects.

AspectDirect Command ExecutionSecure HTTP Endpoint
Server accessRequires shell/SSHWorks on shared hosting
EnvironmentCLI, no web serverFull web environment, sessions available
SecurityRelies on file permissionsNeeds token/HTTPS protection
SchedulingUse crontab directlyUse curl/wget in crontab or external service
Best forSimple scripts, full server controlWeb-dependent scripts, no shell access

For website owners who prefer a managed solution, FreeCronJob blog offers a convenient way to schedule HTTP-based PHP cron jobs without maintaining your own server cron table. You can read more about best practices and setup tips on the FreeCronJob blog. Whichever method you choose, always monitor your logs and test thoroughly after any PHP version update.