使用命令行通过cron定时执行PHP脚本:在Ubuntu系统中编辑crontab,添加如 /usr/bin/php /var/www/html/script.php的规则,实现每分钟自动运行PHP文件。2. 利用Web版Cron服务远程触发PHP脚本:注册EasyCron等服务,配置目标URL(如https://example.com/monitor.php)及执行频率,通过HTTP请求远程调用脚本。3. 在PHP代码中使用sleep函数实现简单轮询:编写while(true)循环并插入sleep(60)暂停间隔,适用于低精度的持续监控任务,需通过CLI保持后台运行。4. 借助现代框架内置调度器管理定时任务:以Laravel为例,在Kernel.php中定义每分钟或每日任务,并设置单一cron入口 cd /path-to-project && php artisan schedule:run由框架自动调度。5. 通过日志文件记录脚本执行情况:在脚本开头使用fopen('log.txt', 'a')写入时间戳和状态信息,如'Script started',便于后续排查问题和验证执行效果。

1. Using Command Line to Execute PHP Scripts Periodically
The operating environment of this tutorial: Dell XPS 13, Ubuntu 22.04
Scheduling PHP scripts via the operating system's built-in task scheduler allows automatic execution at defined intervals. On Linux systems, cron is commonly used to trigger PHP files through the command line.
- Open the crontab editor by running crontab -e in the terminal
- Add a new line specifying the time interval and script path, such as: * * * * * /usr/bin/php /var/www/html/script.php to run every minute
- Save and exit; the system will now execute the PHP file according to the schedule
2. Implementing Timer Tasks with Web-Based Cron Services
External monitoring services can simulate HTTP requests to trigger PHP scripts hosted on web servers. This method avoids direct server access and works well for shared hosting environments where cron access is restricted.
- Register with an online cron service like EasyCron or Cron-job.org
- Enter the full URL of your PHP script (e.g., https://example.com/monitor.php)
- Set the desired execution frequency and activate the job
- The service sends periodic GET requests to execute the script remotely
3. Building Internal Scheduling Logic with Sleep Functions
This approach uses PHP’s sleep() function within a long-running script to create delays between operations. It's suitable for lightweight monitoring tasks that don't require precise timing.
立即学习“PHP免费学习笔记(深入)”;
- Create a loop in your PHP file using while(true) to keep it running continuously
- Place your monitoring logic inside the loop (e.g., checking file sizes, response codes)
- Call sleep(60) after each cycle to pause execution for 60 seconds
- Run the script via CLI and leave it active in the background
4. Leveraging Task Schedulers in Frameworks
Modern PHP frameworks such as Laravel provide built-in task scheduling features that simplify managing recurring commands through a single cron entry pointing to the framework’s scheduler runner.
- Define scheduled jobs inside the app/Console/Kernel.php file using the $schedule object
- Use methods like ->everyMinute(), ->daily(), or custom cron syntax
- Ensure one base cron entry exists: * * * * * cd /path-to-project && php artisan schedule:run
- The framework evaluates all defined tasks and runs those due at the current time
5. Monitoring Script Execution with Log Files
Tracking when and how often scripts execute helps identify failures or performance bottlenecks. Writing timestamps and status updates to log files enables post-execution analysis.
- At the start of your PHP script, open a log file using fopen('log.txt', 'a')
- Write entries including date/time and action taken: fwrite($handle, date('Y-m-d H:i:s') . ' - Script started\n')
- Close the file handle after writing with fclose($handle)
- Review logs regularly to verify correct operation and detect anomalies











