简体   繁体   English

PHP-手动启动cron作业/让服务器代替用户工作

[英]PHP - Start a cron job manually / let the server works instead of the user

I have a quite slow script that need to be executed frequently (about 30 times in a minute) so the user can't execute it and the cron job runs at most every minute. 我有一个非常慢的脚本,需要经常执行(每分钟大约30次),因此用户无法执行它,并且cron作业最多每分钟运行一次。

So, is there a way (using PHP) to let the server works instead of the user? 那么,有没有办法(使用PHP)让服务器代替用户工作?

This is quite easy: Use a flag file 这很简单:使用标志文件

Script running without user interaction (may be started by cron or shell, including PHP shell execute): 无需用户交互即可运行的脚本(可以由cron或shell启动,包括PHP shell执行):

<?php
while (true) {
  while (file_exists('/path/to/flagfile')) sleep(1); //Can even use microsleep
  include ('/path/to/worker/script');
  touch('/path/to/flagfile');
}
?>

Script to trigger it (started from webserver via user interaction) 触发它的脚本(通过用户交互从网络服务器开始)

<?php
@unlink('/path/to/flagfile');
echo "Processing triggered!";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM