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 514593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:32:27+00:00 2026-05-13T07:32:27+00:00

I have php scripts that I have to run on linux as well as

  • 0

I have php scripts that I have to run on linux as well as on Windows servers.
I want to use the same scripts without doing any modifications for those 2 environments.

Theses scripts will be scheduled with cron (on linux) and with the windows scheduler (or other, I don’t care for now) for my Windows environment.

However, some of theses scripts might take several minutes to complete. I just want to prevent the same script from being launched by the scheduler (cron or the windows’s one) before it has finished the last time it was launched.

I’m not sure how to do that..
I want to be sure that the “lock” is released if something goes wrong during the execution, so it is launched again the next time without human intervention.

Maybe with a flock on a dummy file would do the trick, but i’m not sure how to do that.

I also have a MySQL database on theses servers. I thought maybe using a lock on the database side.

1- Start a transaction
2- Insert script name in a table.
3- execution of the script.
4- If successful then delete the row and commit the transaction or simply rollback;

If the script name is in the table, I then could prevent it from running.
If the script execution fails, then Mysql will automatically rollback the transaction so the row does not appear the next time the script is called.

But, in a transaction, is there a way for other connections see the uncommited data? if yes, how?

I also thought using a lock on the row if it is impossible to use the rollback thing..

1- Insert script name in a table if it doesn't already exists.
2- Start a transaction.
2- Select * from Table where script_name FOR UPDATE.
3- execution of the script.
4- If successful then release the lock (rollback or commit).

But my major problem here is with Mysql. Select FOR UPDATE hang until the previous lock is released or if the 50 seconds timeout(innodb_lock_wait_timeout variable) is elapsed. I would like Mysql to tell me right on the spot that my row is locked without affecting the whole Database. That is because the innodb_lock_wait_timeout variable is a global one (not a session one).
Is there another variable that mimic the NO_WAIT clause that is available in Oracle?

Or should I let the script hang 50 seconds without any problem?

What is the best approach on that as I am a php newbie and I don’t want to cause any problems on the server.

Maybe I have another option that I didn’t see..

  • 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-13T07:32:28+00:00Added an answer on May 13, 2026 at 7:32 am

    I solved this problem using… sockets. It you can enable php_sockets extention then try it. Here is code sample:

    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if (false === $socket) {
        throw new Exception("can't create socket: ".socket_last_error($socket));
    }
    ## set $port to something like 10000
    ## hide warning, because error will be checked manually
    if (false === @socket_bind($socket, '127.0.0.1', $port)) {
        ## some instanse of the script is running
        return false;
    } else {
        ## let's do your job
        return $socket;
    }
    

    Bind socket on specific $port is safe operation for concurent execution. Operation system will make sure that there is no other process which bound socket to same port. You just need to check return value.

    If script crashes then operation system will unbind the port automatically.

    This also can be used in any language. I’ve tested it wide on perl and php based projects. It stoped parallel execution even when we’ve added script twice in crontab by mistake.

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

Sidebar

Related Questions

I have a series of PHP scripts that I want to run in a
I have lots of experience of writing php scripts that are run in the
I have two php scripts that need to be run as continuous back ground
I have some long-running CLI PHP scripts that run regularly via cron. I'd like
Firstly, I have built PHP scripts that run as a cron job when I
I have a php script that is run once a minute. The script runs
I have php scripts that do things like register, login, upload. I would like
I have a collection of PHP scripts that are extremely CPU intensive, juggling millions
I have a directory of PHP scripts ( mainly utilities), that is located on
I want to run a php script on christmas that will email my family

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.