Question

I have a program with lots of system commands to handle searching for, examining, and killing processes:

system qq(kill $pid);

and

for my $pid ( qx( pgrep -f "$pgrep_re") ) {
    chomp $pid;
    ...
}

and

my $command_line = qx(ps -o command="" $pid);
chomp $command_line;
....

Not only is this system specific, but I'm depending upon the user to have these particular commands in their path the correct way, leaving me with a system security issue (like some joker setting alias ps="rm -rf *").

I would like to do this stuff in a nice, Perl way which would be less dependent upon all of these system commands and be a bit more platform independent1.

Is there a Perl module? Bonus points for one that does it in a nice object-oriented way and doesn't depend externally with these very same commands.


1. A lot of this deals with using ssh and setting up tunnels, so since Windows doesn't have ssh as a native command, I'm willing to exclude it as long as this works well for other Unix/Linux systems.

Was it helpful?

Solution

  • kill: use the builtin kill (perldoc -f kill)
  • ps: use search.cpan.org, there is UNIX::Process. In linux you could also scan through /proc/
  • pgrep: combine ps with perl pattern matching
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top