Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3992066
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:43:17+00:00 2026-05-20T06:43:17+00:00

I have a script that I want to make sure only one is running,

  • 0

I have a script that I want to make sure only one is running, I am doing:

$fp = fopen("lock.txt", "w");
if (flock($fp, LOCK_EX|LOCK_NB)) { // do an exclusive lock
   //Processing code

   flock($fp, LOCK_UN);
}else{
   echo "Could not lock file!";
}

fclose($fp);

The problem is when I start the second script, it just stays there waiting. If I then stop the first script, the second script will then print “Could Not lock this file”. Why doesn’t the second script just stop immediately and report that message?

If it ran the second script, it would know that the file is locked and should exit. When I watch my processes I can see the second script sat there … it’s just wating.

Thanks.

EDIT: I have just tried a quick and dirty database lock i.e set a running field to -1 and check for it when the script opens. But that doesn’t work. I have also tried using sockets as described: here . It seems like the second file won’t even run … Should I even be worried?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:43:18+00:00Added an answer on May 20, 2026 at 6:43 am
    $fp = fopen("lock.txt", "w");
    $block = 0;
    if (flock($fp, LOCK_EX|LOCK_NB, $block)) { // do an exclusive lock
       sleep(5);
       flock($fp, LOCK_UN);
       echo "Could lock file!";
    }else{
       echo "Could not lock file!";
    }
    
    fclose($fp);
    

    works for me. Try adding the 3’rd parameter


    Edit:
    You may have some session locking problems:
    when you call session_start() it will block(put further requests in a waiting state) any other requests to your script until the original script that started the session has finished. To test this, try either accessing with 2 different browsers, or avoid calling session_start() in this script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Python script that I want to stop executing if a
I have a very simple greasemonkey script that I want to call an already
I'm using $().each() to loop through some items. I want to make sure that
I have a script that appends some rows to a table. One of the
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
I have a script that retrieves objects from a remote server through an Ajax
I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a script that takes a table name and generates a control file
I have a script that renders graphs in gnuplot. The graphs all end up
I have a script that works fine on my test server (using IIS6). The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.