Schedule scripts without using CRON(不使用 CRON 调度脚本)
问题描述
我知道有很多关于使用 CRON 运行 php 文件的帖子.但是,在共享托管的世界中,并且易于用户设置,我不想搞砸.
I know there are many posts about using CRON to run a php file. But, in the world of shared hosting, and ease of setup for a user, I don't want to have to mess with that.
我在网上找到了另一个与套接字有关的解决方案.只是想让每个人都接受这个,并告诉我这是一个好主意还是坏主意.听起来效果不错.
I found another solution online that has to do with sockets. Just wanted to get everyones take on this, and tell me if this is a good or bad idea. Sounds like it works well.
想法?
//Open socket connection to cron.php
$socketcon = fsockopen($_SERVER['HTTP_HOST'],80,$errorno,$errorstr,10);
if($socketcon) {
$socketdata = "GET /cron.php HTTP 1.1
Host: ".$_SERVER['HTTP_HOST']."
Connection: Close
";
fwrite($socketcon,$socketdata);
//Normally you would get all the data back with fgets and wait until $socketcon reaches feof.
//In this case, we just do this:
fclose($socketcon);
} else {
//something went wrong. Put your error handler here.
}
cron.php:
//This script does all the work.
sleep(200);
//To prove that this works we will create an empty file here, after the sleep is done.
//Make sure that the webserver can write in the directory you're testing this file in.
$handle = fopen('test.txt','w');
fclose($handle);
从博客文章中找到脚本:http://syn.ac/tech/13/creating-php-cronjobs-without-cron-and-php-cli/
Found the script from a blog post: http://syn.ac/tech/13/creating-php-cronjobs-without-cron-and-php-cli/
推荐答案
这不是一个糟糕的方法,但您需要确保通过关闭套接字它不仅仅是在脚本完成之前终止脚本.您可以将套接字设置为非阻塞.
It's not a bad method, but you need to ensure that by closing the socket it isn't just terminating the script before it finishes. You can set sockets to non-blocking.
我仍然会使用 cron 作业,即使它有点痛苦.
I would still use a cron job, even if it is a bit of a pain.
这篇关于不使用 CRON 调度脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不使用 CRON 调度脚本
基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01