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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:30:04+00:00 2026-05-12T19:30:04+00:00

This is something i have wondered for a while and decided to ask about

  • 0

This is something i have wondered for a while and decided to ask about it.

We have the function getmypid() which will return the current scripts process id. Is there some kind of function such as

checkifpidexists() in php? I mean a inbuilt one and not some batch script solution.

And is there a way to change a scripts pid?

Some clarification:

I want to check if a pid exists to see if the script is already running so it dont run again, faux cron job if you will.

The reason i wanted to change the pid is so i can set the script pid to something really high such as 60000 and hard code that value so this script can only run on that pid so only 1 instance of it would run

EDIT—-

To help anyone else with this proplem, i have created this class:

class instance {

    private $lock_file = '';
    private $is_running = false;

    public function __construct($id = __FILE__) {

        $id = md5($id);

        $this->lock_file = sys_get_temp_dir() . $id;

        if (file_exists($this->lock_file)) {
            $this->is_running = true;
        } else {
            $file = fopen($this->lock_file, 'w');
            fclose($file);
        }
    }

    public function __destruct() {
        if (file_exists($this->lock_file) && !$this->is_running) {
            unlink($this->lock_file);
        }
    }

    public function is_running() {
        return $this->is_running;
    }

}

and you use it like so:

$instance = new instance('abcd'); // the argument is optional as it defaults to __FILE__

if ($instance->is_running()) {
    echo 'file already running';    
} else {
    echo 'file not running';
}
  • 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-12T19:30:05+00:00Added an answer on May 12, 2026 at 7:30 pm

    In linux, you would look at /proc.

    return file_exists( "/proc/$pid" );
    

    In Windows you could shell_exec() tasklist.exe, and that would find a matching process id:

    $processes = explode( "\n", shell_exec( "tasklist.exe" ));
    foreach( $processes as $process )
    {
         if( strpos( "Image Name", $process ) === 0
           || strpos( "===", $process ) === 0 )
              continue;
         $matches = false;
         preg_match( "/(.*?)\s+(\d+).*$/", $process, $matches );
         $pid = $matches[ 2 ];
    }
    

    I believe what you want to do is maintain a PID file. In the daemons I’ve written, they check a config file, look for an instance of a pid file, get the pid out of the pid file, check to see if /proc/$pid exists, and if not, delete the pid file.

       if( file_exists("/tmp/daemon.pid"))
       {
           $pid = file_get_contents( "/tmp/daemon.pid" );
           if( file_exists( "/proc/$pid" ))
           {
               error_log( "found a running instance, exiting.");
               exit(1);
           }
           else
           {
               error_log( "previous process exited without cleaning pidfile, removing" );
               unlink( "/tmp/daemon.pid" );
           }
       }
       $h = fopen("/tmp/daemon.pid", 'w');
       if( $h ) fwrite( $h, getmypid() );
       fclose( $h );
    

    Process IDs are granted by the OS and one cannot reserve a process id. You would write your daemon to respect the pid file.

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

Sidebar

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.