PHP loop acting as cronjob[ensure only one instance running]

后端 未结 2 775
一向
一向 2020-12-18 14:12

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 ever

2条回答
  •  孤城傲影
    2020-12-18 14:44

    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.

提交回复
热议问题