[NCLUG] Perl/bash SIGINT question

Marcio Luis Teixeira marciot at yahoo.com
Mon Jun 9 18:31:33 MDT 2008


Hi guys,

I'm trying to write a job control GUI in Perl/Tk and I'm running into problems getting it to shut down jobs correctly. The idea is that the perl script starts jobs and then when the user requests it, the script must be able to shut down those jobs, just as if the user had done a Cntl-C. There are a couple variations I wish to support. They should either be plain commands, or shell command lines, and additionally, it should be able to do so on another host via "ssh" (this is my primary use case).

I've gotten it mostly to work, except for the case in which I am running a local script. In that case, the SIGINT is being ignored. As prepared some example code to post here, I found out that a workaround is to "ssh" to "localhost", which is probably good enough what I need to do, but it's sort of hacky. I was curious if anyone here had any insight why "runCommand(3, ...)" below does not work, while "runCommand(5, ...)" does. Ideally, I would like to make it so 3 works.

-- Marcio


--------------------------
sig_test.pl
--------------------------
use Cwd;

$remoteHost = "oxygen";
$licenseToKill = 1;
$pwd = cwd();

runCommand(1, "sleep 20");                            # This works
runCommand(2, "ssh $remoteHost -C sleep 20");         # This works
runCommand(3, "$pwd/sig_test.sh");                    # Does not work
runCommand(4, "ssh $remoteHost -C $pwd/sig_test.sh"); # This works
runCommand(5, "ssh localhost -C $pwd/sig_test.sh");   # This works

sleep(1);

# Send kill to all jobs

if ($licenseToKill) {
  for my $pid ( keys %pid_to_id ) {
    print "Sending SIGINT to $pid\n";
    kill SIGINT, $pid;
  }
}

# Wait for jobs to exit

print "Waiting for children to exit\n";
while (($pid = wait) > 0) {
  print "$pid exited\n";
}

sub runCommand {
  my $id  = shift;
  my $cmd = shift;

  my $pid = fork;
  if($pid) {
    $pid_to_id{$pid} = $id;
    print "$pid: $cmd\n";
  } else {
    exec('/bin/sh', '-c', $cmd);
  }
}

--------------------------
sig_test.sh
--------------------------
#!/bin/sh
sleep 30


      


More information about the NCLUG mailing list