PHP循环充当cronjob [确保只有一个实例运行] [英] PHP loop acting as cronjob[ensure only one instance running]

查看:168
本文介绍了PHP循环充当cronjob [确保只有一个实例运行]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多部分问题的php脚本文件。我创建这个文件,每秒更新数据库。没有其他的建模方法,它必须每秒完成。



现在我正在运行CentOS,我是新的。第一个noob问题是:


如何通过SSH运行一个php文件。我读它只是#php路径到/ myfile.php。但我试着回应一些东西,我不会在文本中看到它。


现在我不认为启动文件将是一个问题。一个问题,我想是,我不知道是否甚至可能,但这里去。


百分之百肯定该文件只运行一次。如果我偶然再次运行文件会发生什么。


我想进一步,如果我每秒实现写一个日志,我可以知道如果一切都运行确定。如果有错误或错误,日志文件将停止。


是使用fopen写入日志文件,关。这不是需要很多时间,是不是有一个更容易的方法在CentOS。


有什么发生时,我运行的文件。是文件在内存中运行,还是使用系统中的文件。它是否响应文件中的更改,例如停止脚本的执行。


我可以实现某种停止机制在文件本身。或者有一个命令我可以用来停止文件。


我知道的另一个选项是实现一个cronjob运行每一分钟。并且这个cronjob执行php文件。 php文件将循环一分钟,更新所需的一切,并终止。我实现了这种方法,但只是使用了一个浏览器。我只是浏览到mu文件,并打开它。我看到浏览器忙了一分钟,但它没有更新数据库中的任何东西。有人知道这是什么原因。



我有另一个问题是通过实现cronjob方法,什么是我填充在PLESK面板的命令。它和上面的命令是一样的。只是php和文件名。或者有像-f -q -something这样的特殊命令。



对于所有的noob问题都很抱歉。



如果有人可以帮助我,我真的很感激。



Ciao!

解决方案

确保脚本只有一个副本正在运行的最简单的方法是使用 flock() 以获取文件锁定。例如:


 <?php 

$ fp = fopen (/tmp/lock.txt,r +);

if(flock($ fp,LOCK_EX)){//做一个独占锁
ftruncate($ fp,0); // truncate file
fwrite($ fp,Write something here \\\
);
flock($ fp,LOCK_UN); //释放锁
} else {
echo无法获取锁!
}

fclose($ fp);

?>


所以基本上你会有一个虚拟文件设置,脚本,启动时,尝试获取锁。如果成功,它运行。如果没有,它退出。

注意: flock() code>是所谓的咨询锁定方法,这意味着它只有在您使用它时才有效。所以这将阻止你自己的脚本多次运行,但不会做任何其他脚本,这听起来很好在你的情况。


I have a multi part question for a php script file. I am creating this file that updates the database every second. There is no other modeling method, it has to be done every second.

Now i am running CentOS and i am new to it. The first noob question is:

How do i run a php file via SSH. I read it is just # php path-to/myfile.php. But i tried to echo something, and i dont see it in the text.

Now i don't think that starting the file is going to be a problem. One problem i guess will be, i don't know if it is even possible, but here goes.

Is it possible for me to be hundred percent sure that the file is only run once. What happens if i by accident run the file again.

I was wondering further, if i implement a write to a log every second, i can know if everything is running ok. If there is an error or something wrong the log file will stop.

Is the writing to a log file with the fopen, and write and close. Isn't this going to take a lot of time, isn't there an easier method in CentOS.

Ok another big point i have is what happens when i run the file. Is the file run in the memory, or does it use the file in the system. Does it respond on changes made in the file, for example to stop the execution of the script.

Can i implement some kind of stop mechanism in the file itself. Or is there a command i can use to stop the file.

Another option i know of is implementing a cronjob that runs every minute. And this cronjob executes the php file. The php file will loop for one minute, updateting everything needed, and terminating. I implemented this method, but just used a browser. I just browsed to mu file, and opened it. I saw the browser was busy for a minute, but it didn't update anything in the database. Does anyone have an idea what the reason of this can be.

Another question i have is by implementing the cronjob method, what is the command i fill in the PLESK panel. Is it the same as the above command. just php and the file name. Or are there special command like -f -q -something.

Sorry for all the noob questions.

If someone can help me i really appreciate it.

Ciao!

解决方案

The simplest way to ensure only one copy of your script is running is to use flock() to obtain a file lock. For example:

<?php

$fp = fopen("/tmp/lock.txt", "r+");

if (flock($fp, LOCK_EX)) { // do an exclusive lock
    ftruncate($fp, 0); // truncate file
    fwrite($fp, "Write something here\n");
    flock($fp, LOCK_UN); // release the lock
} else {
    echo "Couldn't get the lock!";
}

fclose($fp);

?>

So basically you'd have a dummy file set up where your script, upon starting, tries to acquire a lock. If it succeeds, it runs. If not, it exits. That way only one copy of your script can be running at a time.

Note: flock() is what is called an advisory locking method, meaning it only works if you use it. So this will stop your own script from being run multiple times but won't do anything about any other scripts, which sounds fine in your situation.

这篇关于PHP循环充当cronjob [确保只有一个实例运行]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆