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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:56:12+00:00 2026-06-03T20:56:12+00:00

I have one php script, and I am executing this script via cron every

  • 0

I have one php script, and I am executing this script via cron every 10 minutes on CentOS.

The problem is that if the cron job will take more than 10 minutes, then another instance of the same cron job will start.

I tried one trick, that is:

  1. Created one lock file with php code (same like pid files) when
    the cron job started.
  2. Removed the lock file with php code when the job finished.
  3. And when any new cron job started execution of script, I checked if lock
    file exists and if so, aborted the script.

But there can be one problem that, when the lock file is not deleted or removed by script because of any reason.
The cron will never start again.

Is there any way I can stop the execution of a cron job again if it is already running, with Linux commands or similar to this?

  • 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-06-03T20:56:13+00:00Added an answer on June 3, 2026 at 8:56 pm

    Advisory locking is made for exactly this purpose.

    You can accomplish advisory locking with flock(). Simply apply the function to a previously opened lock file to determine if another script has a lock on it.

    $f = fopen('lock', 'w') or die ('Cannot create lock file');
    if (flock($f, LOCK_EX | LOCK_NB)) {
        // yay
    }
    

    In this case I’m adding LOCK_NB to prevent the next script from waiting until the first has finished. Since you’re using cron there will always be a next script.

    If the current script prematurely terminates, any file locks will get released by the OS.

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

Sidebar

Related Questions

I have cron job - php script which is called one time in 5
I have problem with session in cakephp.I have one file chat.php that is in
I have one PHP script that has to start a second script in the
I have a cron job the executes a PHP script. The cron is setup
I am trying to make a Postgres PHP backup script. I have downloaded one
I have a form in a PHP script and one text field. However when
I have one form in a PHP (5.2.9-1) application that causes IIS (Microsoft-IIS/6.0) to
I have three files: one called sql.php witch has a class db that I
I am setting up a cron job to run a single PHP script which
I have a php script that needs to run for quite some time. What

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.