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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:02:44+00:00 2026-05-27T09:02:44+00:00

I have a program that normally run till it terminates, but under some situations,

  • 0

I have a program that normally run till it terminates, but under some situations, it created two children process, and the main process then exits. The children processes will be still running after the main process exited.

I want to call this program in PHP.

Originally, I used the following strategy (not valid php code),

$process=proc_open("python xxx.py");
while (1) {
   sleep(5);
   $status = proc_get_status($process);
   if (!$status['running']) {
       exit_loop;
   }
   if (timeout) {
      proc_terminate($process, 9);
      exit_loop;
   }
}

But then I soon found the bug that the process actually exited immediately that $status[‘running’] is false, but the children process is still running. How to actually wait for all children processes?

2nd question is i haven’t looked into the proc_terminate, but I also observed that proc_terminate may not work as I expected. Is that because the $process is a bash but not the real python process? It may also because that what I terminated is only the parent main process but not the children process.

Anyone advise me that whether the above code is robust PHP code or not? Is there any better way to deal with this?

Thanks.

======================

More info,

  1. I run this program in shell (CLI), and not related to CGI functions. So I won’t need to worry about max_execution_time.

  2. I want to wait the children processes created by the main process opened by proc_open, I will only create one process in the program.

  3. The python program I want to call may hang for very long time, so I want to check timeout and terminate it.

  • 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-27T09:02:45+00:00Added an answer on May 27, 2026 at 9:02 am

    proc_get_status can get pid of child process

    $status = proc_get_status($process);
    $pid = $status["pid"]
    

    then you can use pcntl_waitpid to wait until the child process exit

    pcntl_waitpid($pid, $child_status);
    

    But you need to have your php compiled with pcntl extension.

    See PCNTL Functions

    UPDATE: If you want to kill child process after a timeout

    First: install a SIGALARM handler in your main process

    function term_proc() {
       // terminate your all child process
    }
    
    pcntl_signal(SIGALARM, term_proc);
    

    Second: use pcntl_alarm to send a alarm signal after seconds

    pcntl_alarm($seconds)
    

    For 2nd question, proc_terminate will work no matter what process be opend.

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

Sidebar

Related Questions

I have a program that I need to run under *nix and windows. because
I have a C# project that compiles normally. When I run the project via
I've run into some problems debugging a multi-threaded process using GDB. I have a
I have a program that is constantly running. Normally, it seems to garbage collect,
I have a multi-threaded C++ program that deadlocks in some rare cases. The problem
Hej Hej, I have a .Net program that has to run on a cluster
I have a C++ program that crashes on an access violation when I run
I have program that has a variable that should never change. However, somehow, it
I have program that runs fast enough. I want to see the number of
I have a program that spits out both standard error and standard out, and

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.