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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:03:24+00:00 2026-05-14T07:03:24+00:00

I have a command-line PHP script that runs a wget request using each member

  • 0

I have a command-line PHP script that runs a wget request using each member of an array with foreach. This wget request can sometimes take a long time so I want to be able to set a timeout for killing the script if it goes past 15 seconds for example. I have PHP safemode disabled and tried set_time_limit(15) early in the script, however it continues indefinitely. Update: Thanks to Dor for pointing out this is because set_time_limit() does not respect system() calls.

So I was trying to find other ways to kill the script after 15 seconds of execution. However, I’m not sure if it’s possible to check the time a script has been running while it’s in the middle of a wget request at the same time (a do while loop did not work). Maybe fork a process with a timer and set it to kill the parent after a set amount of time?

Thanks for any tips!

Update:
Below is my relevant code. $url is passed from the command-line and is an array of multiple URLs (sorry for not posting this initially):

foreach( $url as $key => $value){
    $wget = "wget -r -H -nd -l 999 $value";
    system($wget);
    }
  • 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-14T07:03:24+00:00Added an answer on May 14, 2026 at 7:03 am

    You can use a combination of “–timeout” and time(). Start off by figuring out how much time you have total, and lower the –timeout as your script runs.

    For example:

    $endtime = time()+15;
    foreach( $url as $key => $value){
      $timeleft = $endtime - time();
      if($timeleft > 0) {
        $wget = "wget -t 1 --timeout $timeleft $otherwgetflags $value";
        print "running $wget<br>";
        system($wget);
      } else {
        print("timed out!");
        exit(0);
      }
    }
    

    Note: if you don’t use -t, wget will try 20 times, each waiting –timeout seconds.

    Here’s some example code using proc_open/proc_terminate (@Josh’s suggestion):

    $descriptorspec = array(
       0 => array("pipe", "r"),
       1 => array("pipe", "w"),
       2 => array("pipe", "w")
    );
    $pipes = array();
    
    $endtime = time()+15;
    foreach( $url as $key => $value){
      $wget = "wget $otherwgetflags $value";
      print "running $wget\n";
      $process = proc_open($wget, $descriptorspec, $pipes);
      if (is_resource($process)) {
        do {
          $timeleft = $endtime - time();
          $read = array($pipes[1]);
          stream_select($read, $write = NULL, $exeptions = NULL, $timeleft, NULL);
          if(!empty($read)) {
            $stdout = fread($pipes[1], 8192);
            print("wget said--$stdout--\n");
          }
        } while(!feof($pipes[1]) && $timeleft > 0);
        if($timeleft <= 0) {
          print("timed out\n");
          proc_terminate($process);
          exit(0);
        }
      } else {
        print("proc_open failed\n");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have verified that my php script runs from the command line, but it
I have a script that runs on the command line, called by a crontab.
I have a PHP script that needs to run from command line. One of
I have a PHP script that takes to command line arguments. I want the
Specifically I have a PHP command-line script that at a certain point requires input
I have a php script that i run via command line like php file.php
I have a PHP script that dynamically creates a HTML file. In command line,
I have a php script that I called in command line like that :
I have a command line script that uses the Django ORM and MySQL backend.
I have a command line php app that I need to distribute to a

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.