1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Make exec() spawns detect if another is already running?

Discussion in 'PHP' started by Sergeant, Jun 29, 2008.

  1. #1
    I have an image gallery that I'm putting together and I've got a script that is called from the administrative backend through exec(). It searches a "batch" folder for images I've uploaded in bulk for it to automatically add to the database. This process can take a few minutes depending on the number and size of images I've uploaded to said folder, being the reason it must be spawned externally like that.

    But I'd like to know if there's a feasible way to keep from accidentally running another instance of the script as this would surely cause conflicts if another batch process was already running. Ideally it should work like this:

    • Go to Admin panel.
    • Click on Batch Processing.
    • Get a message stating it's currently processing and will be finished shortly.
    • Go to Admin Panel again.
    • See that Batch Processing is greyed out/unclickable to prevent spawning.
    • If it is somehow spawned again, receive an error message another batch process is already running and refuse to run another until the first is finished.

    The simplest way I can figure to do this is make a plaintext "lock file" (maybe call it "batch_lock.txt") that simply contains 1 or 0 that the admin panel and the batch.php looks for. When batch.php is finished, it simply sets the number in the file to Zero so another can.

    Any suggestions are appreciated. :cool:
     
    Sergeant, Jun 29, 2008 IP
  2. clarky_y2k3

    clarky_y2k3 Well-Known Member

    Messages:
    114
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Yeah, a lock file would be the best method and is widely used.
     
    clarky_y2k3, Jun 29, 2008 IP
  3. Sergeant

    Sergeant Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That's what I thought. What I might do is make the external script simply create a zero-byte "batch_lock" file when it runs and deletes it when it finishes. The mere presence of it should be enough. If some error occurs and the batch process is terminated prematurely, I would need a way to reset this lock file manually, just in case. :)
     
    Sergeant, Jun 29, 2008 IP