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

问题描述

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



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

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

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

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

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



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



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



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



Ciao!

解决方案

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

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

注意: 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:

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.

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.

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.

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:

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 [确保只有一个实例运行]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:21