exec, system, passthru problem -- wrong output!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry

    exec, system, passthru problem -- wrong output!

    I've found a problem with exec, passthru, shell_exec & system.

    I'm trying to run the following

    exec("sort -r -n -k2,2 r1.txt > r2.txt")

    with r1.txt being a numeric file. The file looks like this:

    237 291 845 152 585 3
    193 810 173 484 151 3
    366 491 910 265 893 3
    220 631 571 332 104 3
    502 567 854 40 108 3
    639 13 720 101 482 3
    254 676 896 759 720 3
    907 928 377 899 778 3
    324 194 995 161 708 3
    501 936 716 105 209 3

    (The file is actually 1500 lines long, but this gives you an idea.
    It's tab delimited, and the numbers are random: I created the file
    using awk to test this).

    Here's the problem:
    If I run it with exec, passthru, shell_exec or system, THE FILE GETS
    TRUNCATED. Instead of 1500 lines, I get 1338. But if I run the SAME
    COMMAND in the shell (manually), THE SORT WORKS OK!

    I run this in other files and the result is the same: the file gets
    truncated. Also, I checked for special characters, but there are none:
    the file contains just regular numbers and tabs. The problem seems to
    be PHP since when I run the command manually I get correct results...

    Any ideas?

    Thanks.
  • steve

    #2
    Re: exec, system, passthru problem -- wrong output!

    Jerry23 wrote:[color=blue]
    > I’ve found a problem with exec, passthru, shell_exec & system.
    >
    > I’m trying to run the following
    >
    > exec("sort -r -n -k2,2 r1.txt > r2.txt")
    >
    > with r1.txt being a numeric file. The file looks like this:
    >
    > 237 291 845 152 585 3
    > 193 810 173 484 151 3
    > 366 491 910 265 893 3
    > 220 631 571 332 104 3
    > 502 567 854 40 108 3
    > 639 13 720 101 482 3
    > 254 676 896 759 720 3
    > 907 928 377 899 778 3
    > 324 194 995 161 708 3
    > 501 936 716 105 209 3
    >
    > (The file is actually 1500 lines long, but this gives you an idea.
    > It’s tab delimited, and the numbers are random: I created the file
    > using awk to test this).
    >
    > Here’s the problem:
    > If I run it with exec, passthru, shell_exec or system, THE FILE[/color]
    GETS[color=blue]
    > TRUNCATED. Instead of 1500 lines, I get 1338. But if I run the SAME
    > COMMAND in the shell (manually), THE SORT WORKS OK!
    >
    > I run this in other files and the result is the same: the file gets
    > truncated. Also, I checked for special characters, but there are[/color]
    none:[color=blue]
    > the file contains just regular numbers and tabs. The problem seems[/color]
    to[color=blue]
    > be PHP since when I run the command manually I get correct[/color]
    results...[color=blue]
    >
    > Any ideas?
    >
    > Thanks.[/color]
    Sounds impossible. PHP simply passes the command to the shell, so I
    cannot think of a reason it would be truncated. Are you sure, very
    weird. The only thing I can think of is that PHP puts some kind of
    timing contraints on the command. Easy to test that, jut put a " &"
    at the end of the command to have it run in the background, and not
    subject to any timing constraints.

    Another approach is to bring the output into php using the `---` as
    in:
    $a = `sort -r -n -k2,2 r1.txt`;
    and see what $a looks like.

    --
    http://www.dbForumz.com/ This article was posted by author's request
    Articles individually checked for conformance to usenet standards
    Topic URL: http://www.dbForumz.com/PHP-exec-sys...ict125482.html
    Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=418020

    Comment

    • Colin McKinnon

      #3
      Re: exec, system, passthru problem -- wrong output!

      steve wrote:
      [color=blue]
      > Jerry23 wrote:[color=green]
      > >
      > > Here’s the problem:
      > > If I run it with exec, passthru, shell_exec or system, THE FILE[/color]
      > GETS[color=green]
      > > TRUNCATED. Instead of 1500 lines, I get 1338. But if I run the SAME
      > > COMMAND in the shell (manually), THE SORT WORKS OK![/color][/color]
      [color=blue]
      > Sounds impossible. PHP simply passes the command to the shell, so I
      > cannot think of a reason it would be truncated. Are you sure, very
      > weird. The only thing I can think of is that PHP puts some kind of
      > timing contraints on the command. Easy to test that, jut put a " &"
      > at the end of the command to have it run in the background, and not
      > subject to any timing constraints.
      >[/color]

      IIRC that won't remove the constraint - the process will get a SIGHUP on
      timeout of the php script - you'd need to isolate it from the signal (with
      nohup) or run it in a seperate process group (e.g. `at now
      run_shell_comma nd`)

      HTH

      C.

      Comment

      • Jerry

        #4
        Re: exec, system, passthru problem -- wrong output!

        Colin McKinnon <colin.deleteth is@andthis.mms3 .com> wrote in message news:<cc37vv$qp r$1$830fa795@ne ws.demon.co.uk> ...[color=blue]
        > steve wrote:
        >[color=green]
        > > Jerry23 wrote:[color=darkred]
        > > >
        > > > Here?s the problem:
        > > > If I run it with exec, passthru, shell_exec or system, THE FILE[/color][/color]
        > GETS[color=green][color=darkred]
        > > > TRUNCATED. Instead of 1500 lines, I get 1338. But if I run the SAME
        > > > COMMAND in the shell (manually), THE SORT WORKS OK![/color][/color]
        >[color=green]
        > > Sounds impossible. PHP simply passes the command to the shell, so I
        > > cannot think of a reason it would be truncated. Are you sure, very
        > > weird. The only thing I can think of is that PHP puts some kind of
        > > timing contraints on the command. Easy to test that, jut put a " &"
        > > at the end of the command to have it run in the background, and not
        > > subject to any timing constraints.
        > >[/color]
        >
        > IIRC that won't remove the constraint - the process will get a SIGHUP on
        > timeout of the php script - you'd need to isolate it from the signal (with
        > nohup) or run it in a seperate process group (e.g. `at now
        > run_shell_comma nd`)
        >
        > HTH
        >
        > C.[/color]

        Colin, Steve:

        OK Here's the deal: when I run them with & at the end (background), I
        get the correct output. When I run them without the & I get the files
        truncated. It looks like there's a problem with timing constraints as
        you guys suggested.
        I'll be following this problem (post it in some php dev list so
        they're aware of the problem, etc).

        Thank you for your help.

        Jerry.

        Comment

        Working...