<small id='I6hSL'></small><noframes id='I6hSL'>

    <bdo id='I6hSL'></bdo><ul id='I6hSL'></ul>
<tfoot id='I6hSL'></tfoot>
<i id='I6hSL'><tr id='I6hSL'><dt id='I6hSL'><q id='I6hSL'><span id='I6hSL'><b id='I6hSL'><form id='I6hSL'><ins id='I6hSL'></ins><ul id='I6hSL'></ul><sub id='I6hSL'></sub></form><legend id='I6hSL'></legend><bdo id='I6hSL'><pre id='I6hSL'><center id='I6hSL'></center></pre></bdo></b><th id='I6hSL'></th></span></q></dt></tr></i><div id='I6hSL'><tfoot id='I6hSL'></tfoot><dl id='I6hSL'><fieldset id='I6hSL'></fieldset></dl></div>
      1. <legend id='I6hSL'><style id='I6hSL'><dir id='I6hSL'><q id='I6hSL'></q></dir></style></legend>
      2. PHP 循环充当 cronjob[确保只运行一个实例]

        时间:2024-04-13
        <i id='c3wtz'><tr id='c3wtz'><dt id='c3wtz'><q id='c3wtz'><span id='c3wtz'><b id='c3wtz'><form id='c3wtz'><ins id='c3wtz'></ins><ul id='c3wtz'></ul><sub id='c3wtz'></sub></form><legend id='c3wtz'></legend><bdo id='c3wtz'><pre id='c3wtz'><center id='c3wtz'></center></pre></bdo></b><th id='c3wtz'></th></span></q></dt></tr></i><div id='c3wtz'><tfoot id='c3wtz'></tfoot><dl id='c3wtz'><fieldset id='c3wtz'></fieldset></dl></div>

          1. <small id='c3wtz'></small><noframes id='c3wtz'>

              <tbody id='c3wtz'></tbody>
            <tfoot id='c3wtz'></tfoot>

            • <legend id='c3wtz'><style id='c3wtz'><dir id='c3wtz'><q id='c3wtz'></q></dir></style></legend>
                <bdo id='c3wtz'></bdo><ul id='c3wtz'></ul>

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

                  问题描述

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

                  现在我正在运行 CentOS,我是新手.第一个菜鸟问题是:

                  <块引用>

                  如何通过 SSH 运行 php 文件.我读到它只是#php path-to/myfile.php.但是我试图回应一些东西,我没有在文本中看到它.

                  现在我认为启动文件不会有问题.我想一个问题是,我什至不知道这是否可能,但就是这样.

                  <块引用>

                  我是否有可能百分百确定该文件只运行一次.如果我不小心再次运行该文件会发生什么.

                  我还想知道,如果我每秒钟执行一次日志写入,我就可以知道一切是否运行正常.如果出现错误或错误,日志文件将停止.

                  <块引用>

                  是用fopen 写入日志文件,然后写入和关闭.这不是要花很多时间吗,CentOS中没有更简单的方法吗.

                  好的,我的另一个重点是运行文件时会发生什么.文件是在内存中运行,还是使用系统中的文件.它是否响应文件中所做的更改,例如停止脚本的执行.

                  <块引用>

                  我可以在文件本身中实现某种停止机制吗?或者有没有我可以用来停止文件的命令.

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

                  我的另一个问题是通过实施 cronjob 方法,我在 PLESK 面板中填写的命令是什么.是不是和上面的命令一样.只是 php 和文件名.或者有没有特殊的命令,比如 -f -q -something.

                  抱歉所有菜鸟问题.

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

                  Ciao!

                  解决方案

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

                  <块引用>

                  所以基本上你会设置一个虚拟文件,你的脚本在启动时会尝试获取锁.如果成功,它就会运行.如果没有,则退出.这样,您的脚本一次只能运行一个副本.

                  注意: flock() 是所谓的advisory 锁定方法,这意味着它只有在你使用时才有效.因此,这将阻止您自己的脚本多次运行,但不会对任何其他脚本执行任何操作,这在您的情况下听起来不错.

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

                    1. <tfoot id='1H6t2'></tfoot>
                      <i id='1H6t2'><tr id='1H6t2'><dt id='1H6t2'><q id='1H6t2'><span id='1H6t2'><b id='1H6t2'><form id='1H6t2'><ins id='1H6t2'></ins><ul id='1H6t2'></ul><sub id='1H6t2'></sub></form><legend id='1H6t2'></legend><bdo id='1H6t2'><pre id='1H6t2'><center id='1H6t2'></center></pre></bdo></b><th id='1H6t2'></th></span></q></dt></tr></i><div id='1H6t2'><tfoot id='1H6t2'></tfoot><dl id='1H6t2'><fieldset id='1H6t2'></fieldset></dl></div>
                      • <bdo id='1H6t2'></bdo><ul id='1H6t2'></ul>
                        <legend id='1H6t2'><style id='1H6t2'><dir id='1H6t2'><q id='1H6t2'></q></dir></style></legend>

                        <small id='1H6t2'></small><noframes id='1H6t2'>

                          <tbody id='1H6t2'></tbody>