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

  • Home
  • SEARCH
  • 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 355093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:59:09+00:00 2026-05-12T11:59:09+00:00

I know this is a bit generic, but I’m sure you’ll understand my explanation.

  • 0

I know this is a bit generic, but I’m sure you’ll understand my explanation. Here is the situation:


The following code is executed every 10 minutes. Variable “var_x” is always read/written to an external text file when its refereed to.

if ( var_x != 1 )
{
   var_x = 1;
   //
   // here is where the main body of the script is.
   // it can take hours to completely execute.
   //
   var_x = 0;
}
else
{
   // exit script as it's already running.
}

The problem is: if I simulate a hardware failure (do a hard reset when the script is executing) then the main script logic will never execute again because “var_x” will always be “1”. (I already have logic to work out the restore point).

Thanks.

  • 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-12T11:59:09+00:00Added an answer on May 12, 2026 at 11:59 am

    You should lock and unlock files with flock:

    $fp = fopen($your_file);
    if (flock($fp, LOCK_EX)) { )
    {
       //
       // here is where the main body of the script is.
       // it can take hours to completely execute.
       //
        flock($fp, LOCK_UN);
    }
    else
    {
       // exit script as it's already running.
    }
    

    Edit:

    As flock seems not to work correctly on Windows machines, you have to resort to other solutions. From the top of my head an idea for a possible solution:

    Instead of writing 1 to var_x, write the process ID retrieved via getmypid. When a new instance of the script reads the file, it should then lookup for a running process with this ID, and if the process is a PHP script. Of course, this can still go wrong, as there is the possibility of another PHP script obtaining the same PID after a hardware failure, so the solution is far from optimal.

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

Sidebar

Related Questions

I know this is a bit bleeding edge, but here's the question anyway: Given
I know this topic is bit old, but i did surf the web and
I know this might be a bit awkward but I am trying to modify
What does this bit of code represent? I know it's some kind of if
I know this has a stupid solution but, sorry, I'm little bit confused. I
This might sound a bit of an odd question but I know what I
I know this question has been partly answered here on S.O. , but there
I'm sorry if this is a bit of a C-noob question: I know I
This one is a huge issue: first off, while I know a little bit
Know this might be rather basic, but I been trying to figure out how

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.