计划脚本而不使用CRON [英] Schedule scripts without using 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\r\nHost: ".$_SERVER['HTTP_HOST']."\r\nConnection: Close\r\n\r\n";
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-和-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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!