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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:28:33+00:00 2026-06-03T07:28:33+00:00

I am writing a php script which will run through cron . Its task

  • 0

I am writing a php script which will run through cron . Its task is to call other php scripts. The called scripts can also be executed externally. What I want is that while one file is executing, it should not be executed concurrently. To make things clear, consider following

Suppose there is one script master.php which calls to two other scripts script1.php and script2.php one after another. Now, once it called script1.php, it starts executing and take 10 mins to finish its processing. This Script1.php can also be run by user separately in these 10 mins.

What I want to do is while this script is amid its processing, it should not execute only parallely.

Is there a way to achieve this ? (Perhaps by checking the PID of script1.php).

Thanx in advance.

  • 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-03T07:28:34+00:00Added an answer on June 3, 2026 at 7:28 am

    This is how I do it with a pid file. Also if the script is running for more than 300 seconds, it will be killed:

    $pid_file = '/var/run/script1.pid';
    if (file_exists($pid_file)) {
            $pid = file_get_contents($pid_file);
            if ($pid && file_exists('/proc/' . $pid)) {
                    $time = filemtime($pid_file);
                    if (time() - $time > 300) {
                            posix_kill($pid, 9);
                    } else {
                        exit("Another instance of {$argv[0]} is running.\n");
                    }
            }
    }
    
    $pid = posix_getpid();
    if (!file_put_contents($pid_file, $pid)) exit("Can't write pid file $pid_file.\n");
    

    And then unlink the pid file at the end of the script:

    unlink($pid_file);
    

    If you want to run the script when the first instance finish – replace the exit() with sleep(1) and put the whole block in a loop.

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

Sidebar

Related Questions

I am writing a PHP script for a client which will be installed on
I'm writing a php script which will enable people to change the theme of
I am writing a PHP script (which also uses linux bash commands) which will
I'm currently writing a PHP script for a guestbook in PHP where people can
I'm writing a php code which is meant to run from the Unix console,
I'm writing a php script that will fetch data from a given URL and
I am writing a bunch of PHP classes which will be used to create
Im writing a php script to compare the similarity of 2 strings. This works
I am writing a PHP script to add items to a shopping basket. My
I'm writing a php script to crop an image. The script overwrites the 'old'

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.