PHP loop acting as cronjob[ensure only one instance running](PHP循环充当cronjob[确保只运行一个实例])
问题描述
我有一个关于 php 脚本文件的多部分问题.我正在创建这个每秒更新数据库的文件.没有其他的建模方法,它必须每一秒都在做.
现在我正在运行 CentOS,而且我是新手.第一个菜鸟问题是:
<块引用>我如何通过 SSH 运行 php 文件.我读到它只是#php path-to/myfile.php.但是我试图回应一些东西,但我没有在文本中看到它.
现在我认为启动文件不会有问题.我猜的一个问题是,我不知道这是否可能,但这里有.
<块引用>我是否可以百分百确定该文件只运行一次.如果我不小心再次运行文件会发生什么.
我还想知道,如果我每秒钟都执行一次写入日志,我就可以知道一切是否运行正常.如果出现错误或错误,日志文件将停止.
<块引用>是用fopen 写入日志文件,并写入和关闭.这不是要花很多时间吗,CentOS 有没有更简单的方法.
好的,我的另一个重点是运行文件时会发生什么.文件是运行在内存中,还是使用了系统中的文件.它是否响应文件中所做的更改,例如停止执行脚本.
<块引用>我可以在文件本身中实现某种停止机制吗?或者有没有我可以用来停止文件的命令.
我知道的另一个选择是实现每分钟运行一次的 cronjob.这个 cronjob 执行 php 文件.php 文件将循环一分钟,更新所需的一切,然后终止.我实现了这个方法,但只是使用了浏览器.我只是浏览到 mu 文件,然后打开它.我看到浏览器忙了一分钟,但它没有更新数据库中的任何内容.有没有人知道这是什么原因.
我的另一个问题是通过实施 cronjob 方法,我在 PLESK 面板中填写的命令是什么.是不是和上面的命令一样.只是 php 和文件名.或者有没有像 -f -q -something 这样的特殊命令.
抱歉所有菜鸟问题.
如果有人能帮助我,我真的很感激.
呸!
确保只有一个脚本副本正在运行的最简单方法是使用 flock()
获取文件锁.例如:
所以基本上你会设置一个虚拟文件,你的脚本在启动时会尝试获取锁.如果成功,它就会运行.如果没有,它退出.这样一次只能运行一个脚本副本.
注意: flock()
是所谓的建议锁定方法,这意味着它只有在您使用时才有效.因此,这将阻止您自己的脚本多次运行,但不会对任何其他脚本执行任何操作,这在您的情况下听起来不错.
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 "); 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[确保只运行一个实例]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP循环充当cronjob[确保只运行一个实例]
基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01