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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:20:16+00:00 2026-05-28T07:20:16+00:00

Ealier I used function in a seperate file that could be call by script

  • 0

Ealier I used function in a seperate file that could be call by script for cron.

function processExists($file = false) {

   $exists     = false;
   $file       = $file ? $file : basename(__FILE__);

   $command = "ps -ef | grep -v grep | grep $file";
   exec($command, $pids);
   if (count($pids) > 1) {
      $exists = true;
   }
   return $exists;
}

It could be executed from any php script as:

if (processExists(basename(__FILE__))) {
   echo 'Process in already running ';
   exit(0);
}

For some reason it doesn’t work in Debian6.
Now I add in the beginning of every script:

$fh = fopen(__FILE__, 'r');
if (!flock($fh, LOCK_EX | LOCK_NB)) {
   echo 'Script is already running!!!' . "\n";
   exit(0);
}

It causes duplication of code in every script I tried to create function in a seperate file to call from any php script when neccessary, for example something like this:

function stopIfRunning($file)
{
    $fh = fopen($file, 'r');
    if(!flock($fh, LOCK_EX | LOCK_NB)) {
        echo 'Script is already running!!!' . "\n";
        exit(0);
    }
}

And calling from php script:

stopIfRunning(__FILE__);

But it doesn’t work in this case. Could you please explain why it doesn’t work in this case?

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

    It seems the problem is with $fh on stopIfRunning function. If the file with with functiona changed it with:

    $fh = null;
    function stopIfRunning($file)
    {
        global $fh;
        echo $file, "\n";
        $fh = fopen($file, 'r');
        if (!$fh) {
            echo 'Failed to open file', "\n";
        }
        if(!flock($fh, LOCK_EX | LOCK_NB)) {
            echo 'Script is already running!!!' . "\n";
            exit(0);
        }
    }   
    

    And call it in the file that should be launched using cron:

    stopIfRunning(__FILE__);
    

    Here global variable is used, which is not good. It would be great if somebody suggest a solution without global variable.

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

Sidebar

Related Questions

I have a recursive function (in C#) that I need to call about 800
When you open a solution in Visual Studio 2008 (or ealier versions for that
I have a script that I want to be injected into the document BEFORE
This is my current function below. Its used to create a folder in a
When writing a Javascript a function that I had gotten help from earlier ,
In a function that reads data (data meaning exclusively strings) from disk, which should
I have a DLL with one single function That gets five doubles and one
While reading the LearnYouSomeErlang and I found that cons operator is used to get
Here is some CRTP based template code that I used to try and resolve
In unmanaged C++ I have a function which I'm trying to call from C#.

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.