Question

How can I determine from a Perl script if it's running through SGE or locally from the command line?

Was it helpful?

Solution

From http://www.cbi.utsa.edu/sge_tutorial:

When a Sun Grid Engine job is run, a number of variables are preset into the
job’s script environment, as listed below.

SGE_ROOT - The Sun Grid Engine root directory as set for sge_execd before
   start-up or the default /usr/SGE
SGE_CELL - The Sun Grid Engine cell in which the job executes
SGE_JOB_SPOOL_DIR - The directory used by sge_shepherd(8) to store jobrelated
   data during job execution
...
...

Checking %ENV should tell you if it is run through SGE.

OTHER TIPS

You can try something like this:

use Proc::ProcessTable;

my $running_under_sge = 0;
my $ppid = getppid();
my $t = new Proc::ProcessTable;
foreach my $p (@{$t->table}) {
    if ($p->pid == $ppid) {
        if ($p->cmdline =~ m/sge_shepherd/) {
            $running_under_sge++;
            last;
        }
    }
}

This works by checking the parent process of your program. If the parent process is sge_shepherd, then it was started by the SGE exec daemon. Depending on what version of Grid Engine you're using, the name might be different.

The script provided by jlp is a thorough way of checking and would be my recommended way of doing it. Though if you want to do just a quick statement to check you if the environment has an SGE job id. See below script.

#!/usr/bin/perl

use strict;

my $job_id = $ENV{JOB_ID};

if ($job_id > 0) {
  print "Is and SGE job";
} else {
  print "Is NOT and SGE job";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top