3

I have written a Perl script which executes a task as per the schedule. Sometimes the task runs for 3 hours, and sometimes for 9 hours. The status of the task changes to COMPLETE after the task is done.

We have an internal CLI command which displays the status of the task. I'd like to implement a polling mechanism whereby the script checks the status of the task periodically and returns PASS once the status changes to COMPLETE.

One option is to run the command in a loop and check the status periodically. I was wondering if there are any other better or more reliable ways to check the status periodically. Perhaps implementing a scheduler or something on those lines.

Does anyone have any suggestions?

Borodin
  • 126,100
  • 9
  • 70
  • 144
user3587025
  • 173
  • 1
  • 4
  • 17

1 Answers1

0

If you want to check on the status of the Perl program before it ends, you could do it with signals. Note that the following code is very simplistic and there are a lot of things to be careful about with signals and Perl, particularly older versions of Perl.

First, define a signal handler function for the signal you want to use. If you are referencing any variables that might be used elsewhere in the program, make sure those variables are in scope. The following example used local to dynamically scope $i so that it will be available in the USR1 signal hander:

$SIG{USR1} = sub { print STDOUT "In USR1 signal handler.  \$i is $i\n"; };

my $j;
for (local $i = 1; $i < 1000000000; $i++) {
  $j = $i*2;
  }

Then, whenever you want to query the running process for status, get the PID and send it a SIGUSR1 signal. You could do this from inside another program, but the following example shows it from command-line:

mb:~ doug$ kill -SIGUSR1 8492

With the signal handler shown above, when the program receives the signal it will output to its STDOUT something like this:

In USR1 signal handler.  $i is 162181697

After Perl executes the signal handler it will go back to what it was doing before it was interrupted.

ratsbane
  • 890
  • 6
  • 7
  • 1
    Why are you messing with SIGIO when there's two perfectly good signals for "user space" in the form of SIGUSR1 and 2? – Sobrique Mar 10 '15 at 00:10