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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:42:06+00:00 2026-06-09T16:42:06+00:00

I have a script to limit the execution time length of commands. limit.php <?php

  • 0

I have a script to limit the execution time length of commands.

limit.php

<?php
declare(ticks = 1);

if ($argc<2) die("Wrong parameter\n");
$cmd = $argv[1];
$tl = isset($argv[2]) ? intval($argv[2]) : 3;

$pid = pcntl_fork();
if (-1 == $pid) {
    die('FORK_FAILED');
} elseif ($pid == 0) {
    exec($cmd);
    posix_kill(posix_getppid(), SIGALRM);
} else {
    pcntl_signal(SIGALRM, create_function('$signo',"die('EXECUTE_ENDED');"));
    sleep($tl);
    posix_kill($pid, SIGKILL);
    die("TIMEOUT_KILLED : $pid");
}

Then I test this script with some commands.

TEST A

php limit.php "php -r 'while(1){sleep(1);echo PHP_OS;}'" 3

After 3s, we can find the processes were killed as we expected.

TEST B

Remove the output code and run again.

php limit.php "php -r 'while(1){sleep(1);}'" 3

Result looks not good, the process created by function “exec” was not killed like TEST A.

[alix@s4 tmp]$ ps aux | grep whil[e]
alix      4433  0.0  0.1 139644  6860 pts/0    S    10:32   0:00 php -r while(1){sleep(1);}

System info

[alix@s4 tmp]$ uname -a
Linux s4 2.6.18-308.1.1.el5 #1 SMP Wed Mar 7 04:16:51 EST 2012 x86_64 x86_64 x86_64 GNU/Linux
[alix@s4 tmp]$ php -v
PHP 5.3.9 (cli) (built: Feb 15 2012 11:54:46) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

Why the processes killed in TEST A but not in TEST B? Does the output impact the SIGKILL?

Any suggestion?

  • 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-09T16:42:07+00:00Added an answer on June 9, 2026 at 4:42 pm

    There is a PIPE between php -r 'while(1){sleep(1);echo PHP_OS;} (process C) and it’s parent (process B), posix_kill($pid, SIGKILL) sends KILL signal to process B, then process B is terminated, but process C doesn’t know anything about the signal and continues to run and outputs something to the broken pipe, when process C receives the SIGPIPE signal but has no idea how to handle it so it exits.

    You can verify it with strace (run php limit.php "strace php -r 'while(1){sleep(1); echo PHP_OS;};'" 1), and you will see something like this:

    14:43:49.254809 write(1, "Linux", 5)    = -1 EPIPE (Broken pipe)
    14:43:49.254952 --- SIGPIPE (Broken pipe) @ 0 (0) ---
    14:43:49.255110 close(2)                = 0
    14:43:49.255212 close(1)                = 0
    14:43:49.255307 close(0)                = 0
    14:43:49.255402 munmap(0x7fb0762f2000, 4096) = 0
    14:43:49.257781 munmap(0x7fb076342000, 1052672) = 0
    14:43:49.258100 munmap(0x7fb076443000, 266240) = 0
    14:43:49.258268 munmap(0x7fb0762f3000, 323584) = 0
    14:43:49.258555 exit_group(0)           = ?
    

    As to php -r 'while(1){sleep(1);}, because there is no broken pipe occurs after it’s parent dies, so it continues to run as expected.

    Generally speaking, you should kill the whole process group but not only the process itself if you want to kill it’s children too, with PHP you can add process B to its own process group, and kill the whole group then, here is the diff with your code:

    --- limit.php   2012-08-11 20:50:22.000000000 +0800
    +++ limit-new.php   2012-08-11 20:50:39.000000000 +0800
    @@ -9,11 +9,13 @@
     if (-1 == $pid) {
         die('FORK_FAILED');
     } elseif ($pid == 0) {
    +    $_pid = posix_getpid();
    +    posix_setpgid($_pid, $_pid);
         exec($cmd);
         posix_kill(posix_getppid(), SIGALRM);
     } else {
         pcntl_signal(SIGALRM, create_function('$signo',"die('EXECUTE_ENDED');"));
         sleep($tl);
    -    posix_kill($pid, SIGKILL);
    +    posix_kill(-$pid, SIGKILL);
         die("TIMEOUT_KILLED : $pid");
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP Script, it needs execution time of at least 1000 seconds
I have few doubts about maximum execution time set in php.ini. Assuming max_execution_time is
I have a PHP script that will run for a long time. I have
I'm trying to limit the max execution time of shell_exec in PHP to say
I have a PHP class, once called, sets the time limit to 60 seconds.
I have a PHP script that runs once a day, and it takes a
I have a PHP script running every hour or so. The script does a
I make 7 get requests at the same time to one PHP script -
I have a hefty PHP script. So much so that I have had to
I have a php script that is reading a remote CSV file, and adding

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.