cli130
cli130

Reputation: 349

how to investigate stuck process - perl script

I have some trouble with stuck process. I did some investigation, but I just could not find the root cause.

I use ps aux | grep scriptname to find the PID. And I did strace, it just stuck on read(5,

sudo strace -p 1939
Process 1939 attached - interrupt to quit
read(5,  <unfinished ...>
Process 1939 detached

I checked fd file in /proc:

sudo ls -l /proc/1939/fd | grep 5
Oct  9 21:20 5 -> pipe:[31041225]

I also used lsof, but I didn't get any clue from it:

/usr/sbin/lsof | grep 31041225
perl5.8.8  1939    5r     FIFO                0,6       0t0   31041225 pipe
perl5.8.8  2252    1w     FIFO                0,6       0t0   31041225 pipe
perl5.8.8  2252    2w     FIFO                0,6       0t0   31041225 pipe
bash       2290    2w     FIFO                0,6       0t0   31041225 pipe
bash       2298    2w     FIFO                0,6       0t0   31041225 pipe
original-  2302    2w     FIFO                0,6       0t0   31041225 pipe

But for my guess, it could be caused by the following code,

sub runScript {
   my $scriptName = $_[0];
   open(DATA, join(" ", @_) . " 2>&1 |");
   while (<DATA>) { 
      print $_;
   }
   close DATA;
}

Need some thoughts on what would be the next step...

Upvotes: 1

Views: 1067

Answers (1)

user1919238
user1919238

Reputation:

Not having error checking on open is a serious problem. Nevertheless, the code you have posted works with those arguments:

#!/usr/bin/perl
use warnings;
use strict;

runScript('ls','-l');

print "done!";

sub runScript {
   my $scriptName = $_[0];
   open(DATA, join(" ", @_) . " 2>&1 |") or die "Can't open filehandle: $!";
   while (<DATA>) { 
      print $_;
   }
   close DATA;
}

Calling this subroutine with different arguments would potentially cause your script to hang, though. For example, run any command that waits for user input:

runScript('vim');

Now you will hang forever.

Upvotes: 1

Related Questions