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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:54:36+00:00 2026-06-04T20:54:36+00:00

I have seen a PHP code that uses shell commands to determinate if this

  • 0

I have seen a PHP code that uses shell commands to determinate if this process is running now, but I’ve lost it. Can you give me a hint how to do this?

The idea is to create a php file and run this file as a cron job using php -f. The execution time of the script may be 10 seconds, but it also may be 10 minutes. I need to make this cron run once every minute and if the cron from the prevous minute is still running – to stop the new one and let the other one finish.

I’m not very good in shell programming so I need some help.

P.S The idea is to create a daemon without putting an endless PHP process in background.

  • 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-04T20:54:37+00:00Added an answer on June 4, 2026 at 8:54 pm

    for many purposes, the following code is ok (there may be a race condition, but for a minutely cronjob this is probably very rare):

    if (file_exists($lockfilename)) {
       ... // lock is already taken
    } else {
       if ( !file_put_contents($lockfilename, $lockstring)) {
          error("unable to write $lockfilename");
       }
       $lock_taken = true;
       ... // do what you have to do
       if ( !unlink($lockfilename)) {
          warning("unable to unlink $lockfilename");
       }
       $lock_taken = false;
    }
    

    The global variable $lock_taken may be useful to make sure that the lockfile is removed whenever execution is halted. The $lockstring could be a date or a Unix timestamp, which may be useful for removing the lock if it is clearly too old (but filemtime() could be enough).

    If you absolutely cannot risk the race condition, you have to do something like

    if ( !($lockfile = fopen($lockfilename, "x"))) {
       ... // lock is already taken
    } else {
       $lock_taken = true;
       ... // write something to lockfile (optional)
       fclose($lockfile);
       ... // do what you have to do
       if ( !unlink($lockfilename)) {
          warning("unable to unlink $lockfilename");
       }
       $lock_taken = false;
    }
    

    This only works for local lockfiles, though.

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

Sidebar

Related Questions

I have seen code like this: function($cfg) use ($connections) {} but php.net doesn't seem
I am not strong on my PHP knowledge, but I have never seen this
I've seen some PHP applications use lines of code that are like this: throw
I have found many sites that describes PRG, but no simple PHP code example.
I have seen code for summation of an array using threads. In this code
I have a php script that sends an email. It currently uses the php
I have a homepage that contains a huge amount of php code to populate
I got some legacy code that has this: <?PHP if(isset($_GET['pagina'])==homepage) { ?> HtmlCode1 <?php
We have a really old legacy code base that uses globals like they're going
I have some C++ code (segment seen below), I need to convert this to

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.