SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Checking if a script is already running...

    A user runs a PHP script (A) from my website. I want this script to check if another script (B) is already running on the server. If not, then script A should execute script B in a way in which the script continues to run on the server without the user having having to wait for it to finish.

    What is the best way to do this?


    (please let me know if this is not clear enough)

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use require_once that checks if the code from a file has already been included
    require_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ernie1 View Post
    you can use require_once that checks if the code from a file has already been included
    require_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.
    I need script A to check if script B is currently running at all on my server.

    Let's say script A uploads a video to a folder, and script B works on the server to encode all the videos in that folder one-by-one.

    If script A places a video in that folder and script B is already running, then script B will automatically encode the video once it is finished with the other videos in the queue. If script B is not running, then that means no other videos are being encoded and script B must be executed on the server to encode the new video.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Use file locking. If file B is locked it is running, when file B is done running it unlocks itself. That would the easiest way I could think of.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can keep active sessions for pages, as they're users. keep active pages in a table and delete that row as page completes. also delete that row with some frequency like 10 mins or 15 mins. so crashed pages can be also survived.

  6. #6
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Use file locking. If file B is locked it is running, when file B is done running it unlocks itself. That would the easiest way I could think of.
    Okay, I'm not familiar with this at all, but I'll give it a shot.
    Let me know how wrong I am here...

    In script A, I would use something like:
    PHP Code:
    if (!(fopen("scriptB.php""r"))) {
    exec("scriptB.php");

    And in the beginning of script B, I would have:
    PHP Code:
    flock(fopen("scriptB.php""r"), LOCK_EX); 
    ...I have no idea what I am doing.

  7. #7
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this I can't test it on windows
    my mobile portal
    ghiris.ro

  8. #8
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also found this tutorial:
    http://www.softwareprojects.com/reso...ning-1288.html

    But how do I execute script B from within script A so that B continues to run on the server without the user having to wait for it to finish?

  9. #9
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there's a solution to your problem, ignore_user_abort, but I don't know if you can use this combination
    my mobile portal
    ghiris.ro

  10. #10
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ernie1 View Post
    there's a solution to your problem, ignore_user_abort, but I don't know if you can use this combination
    Hmm... that's an interesting thought...


    I did some Googling and found this:
    http://andrewensley.com/2009/06/php-...without-abort/

    It seems like it should work.
    I'm going to give it try later; I'll keep the thread updated.


    Thanks for the idea. =D


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •