Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detecting status of job daemon 2

Status
Not open for further replies.

ksbrace

Programmer
May 13, 2000
501
US
Hello,
I have been asked to write a script to detect the status of job daemons. I was wondering if Perl is the best way and if there is a site that might already have this script? thanks

 
@results=`ps -whatever switches`
foreach (@results) {
(@items=split (/ /,$_);
foreach $item (@items) {
print "$item\t";
}
print "\n";
}

Should be a good starting point
HTH
--Paul
 
Paul,
Thanks for the start. I have the listing of jobs. I would like to truncate the listing to just the job name instead of entire path. For example, my script returns:
0:00 /test/a59_5/dir/exe/jobd ama_prod

I would just like it to return ama_prod. Is there a Right function or something that would truncate everything before jobd? thanks again!

 
replace
(@items=split (/ /,$_);
foreach $item (@items) {
print "$item\t";
}
with
(@items=split (/ /,$_);
print "$item[0]\t$item[2]\n";

HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
ok, thanks for your help. I have the correct list of jobs that I wanted to get. Now I need to cross check them w/the master list to determine if they are running or not. I'm just not sure how to exit the for loop if it's found. This is what I have:
Code:
#!/bin/perl
@results = `ps -ef | grep jobd`;
@testJobs = ("aua_prod","aua_dev","alf_prod","alf_dev","bsc_prod","bsc_dev","cer_prod","cer_dev","cng_prod","cng_dev","can_prod","can_dev","del_prod","del_dev","eri_prod","eri_dev","fin_prod","fin_dev","fre_prod","fre_dev","gen_prod","gen_dev","gcc_prod","gcc_dev","jam_prod","jam_dev","jef_prod","jef_dev","mon_prod","mon_dev","one_prod","one_dev","pot_prod","pot_dev","pur_prod","pur_dev","rcc_prod","rcc_dev","sth_prod","sth_dev","sul_prod","sul_dev","uls_prod","uls_dev","exl_dev")
@stopJobs;
foreach $item (@results) {
  @runningJobs;
  $job = substr($item,-9,9);
  $job =~ s/^\s+(.*?)\s+$/$1/;
  $job =~ s/\s//g;
  push(@runningJobs,$job);

  &checkJobs(@runningJobs);
}

sub checkJobs(@rJobs) {
    $flag = false;
    for($i = 0; $i < $#testJobs; $i++; ) {

         for($j = 0; $j < $#rJobs; $j++; ) {
    #foreach $tJob (@testJobs){
        #foreach $rJob ( @rJobs) {
          if($testJobs[$i] == $rJobs[$j]) {
            $flag = true;
          }#if


         }#for
          if ($flag != true) {
             push(@stoppedJobs, $testJobs[$i]);
          }
    }#for
}#checkJobs()
#print &quot;@stoppedJobs \n&quot;;


 
What loop are you trying to stop???

on another note...
Double loops aren't necessary.

foreach $job (@testJobs){
unless( grep /$job/, @rJobs ){
push @stoppedJobs, $job;
}
}
 
ok, thanks for the help on the unless. I'm now getting two compilation errors on the red lines.
Code:
syntax error at detectJobs.pl line 5, near &quot;$item (&quot;
syntax error at detectJobs.pl line 13, near &quot;}&quot;

[\code]
#!/bin/perl
@results = `ps -ef | grep jobd`;
@testJobs = (&quot;aua_prod&quot;,&quot;aua_dev&quot;,&quot;alf_prod&quot;,&quot;alf_dev&quot;,&quot;bsc_prod&quot;,&quot;bsc_dev&quot;,&quot;cer_prod&quot;,&quot;cer_dev&quot;,&quot;cng_prod&quot;,&quot;cng_dev&quot;,&quot;can_prod&quot;,&quot;can_dev&quot;,&quot;del_prod&quot;,&quot;del_dev&quot;,&quot;eri_prod&quot;,&quot;eri_dev&quot;,&quot;fin_prod&quot;,&quot;fin_dev&quot;,&quot;fre_prod&quot;,&quot;fre_dev&quot;,&quot;gen_prod&quot;,&quot;gen_dev&quot;,&quot;gcc_prod&quot;,&quot;gcc_dev&quot;,&quot;jam_prod&quot;,&quot;jam_dev&quot;,&quot;jef_prod&quot;,&quot;jef_dev&quot;,&quot;mon_prod&quot;,&quot;mon_dev&quot;,&quot;one_prod&quot;,&quot;one_dev&quot;,&quot;pot_prod&quot;,&quot;pot_dev&quot;,&quot;pur_prod&quot;,&quot;pur_dev&quot;,&quot;rcc_prod&quot;,&quot;rcc_dev&quot;,&quot;sth_prod&quot;,&quot;sth_dev&quot;,&quot;sul_prod&quot;,&quot;sul_dev&quot;,&quot;uls_prod&quot;,&quot;uls_dev&quot;,&quot;exl_dev&quot;)

[COLOR=red]foreach $item (@results) {[/color]
  @runningJobs;
  $job = substr($item,-9,9);
  $job =~ s/^\s+(.*?)\s+$/$1/;
  $job =~ s/\s//g;
  push(@runningJobs,$job);
  &checkJobs(@runningJobs);
 print &quot;@stoppedJobs \n&quot; ;
[COLOR=red]}[/color]

sub checkJobs(@rJobs) {
    foreach $job (@testJobs){
      unless( grep /$job/, @rJobs ){
        push @stoppedJobs, $job;
      }
    }
}
 
ahh, i see what you mean now by stopping the loop
This should do it....

sub checkJobs{
my (@rJobs) = @_;
foreach $job (@testJobs){
unless( grep /$job/, @rJobs ){
push @stoppedJobs, $job;
}
}
}


 
Code:
#!/bin/perl
@results = `ps -ef | grep jobd`;
@testJobs = (&quot;aua_prod&quot;,&quot;aua_dev&quot;,&quot;alf_prod&quot;,&quot;alf_dev&quot;,&quot;bsc_prod&quot;,&quot;bsc_dev&quot;,&quot;cer_prod&quot;,&quot;cer_dev&quot;,&quot;cng_prod&quot;,&quot;cng_dev&quot;,&quot;can_prod&quot;,
&quot;can_dev&quot;,&quot;del_prod&quot;,&quot;del_dev&quot;,&quot;eri_prod&quot;,&quot;eri_dev&quot;,&quot;fin_prod&quot;,&quot;fin_dev&quot;,&quot;fre_prod&quot;,&quot;fre_dev&quot;,&quot;gen_prod&quot;,&quot;gen_dev&quot;,
&quot;gcc_prod&quot;,&quot;gcc_dev&quot;,&quot;jam_prod&quot;,&quot;jam_dev&quot;,&quot;jef_prod&quot;,&quot;jef_dev&quot;,&quot;mon_prod&quot;,&quot;mon_dev&quot;,&quot;one_prod&quot;,&quot;one_dev&quot;,&quot;pot_prod&quot;,
&quot;pot_dev&quot;,&quot;pur_prod&quot;,&quot;pur_dev&quot;,&quot;rcc_prod&quot;,&quot;rcc_dev&quot;,&quot;sth_prod&quot;,&quot;sth_dev&quot;,&quot;sul_prod&quot;,&quot;sul_dev&quot;,&quot;uls_prod&quot;,&quot;uls_dev&quot;,
&quot;exl_dev&quot;)
#would probably be best to read these jobs from an external file, so it can be updated by an external interface

foreach $item (@results) {
  @runningJobs;                   #what are you doing here, redeclaring an array?
  $job = substr($item,-9,9);      #the substr will be quicker than split
  $job =~ s/^\s+(.*?)\s+$/$1/;    # 
  $job =~ s/\s//g;                #
  push(@runningJobs,$job);        #

  &checkJobs(@runningJobs);
}

sub checkJobs(@rJobs) {
  $flag = false;
  foreach $job (@rJobs) {
    foreach $tjob (@testJobs) {
      if ($tjob eq $job) {
        push (@stoppedJobs, $job);
      }
    }
  }
}
Should do it

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Paul,
Thanks. are you saying I don't need to put the array declaration inside the foreach loop or I don't need the declaration at all?

 
#!/bin/perl
@results = `ps -ef | grep jobd`;
@runningJobs = ();
@testJobs = (&quot;aua_prod&quot;,&quot;aua_dev&quot;,&quot;alf_prod&quot;,&quot;alf_dev&quot;,&quot;bsc_prod&quot;,&quot;bsc_dev&quot;,&quot;cer_prod&quot;,&quot;cer_dev&quot;,&quot;cng_prod&quot;,&quot;cng_dev&quot;,&quot;can_prod&quot;,
&quot;can_dev&quot;,&quot;del_prod&quot;,&quot;del_dev&quot;,&quot;eri_prod&quot;,&quot;eri_dev&quot;,&quot;fin_prod&quot;,&quot;fin_dev&quot;,&quot;fre_prod&quot;,&quot;fre_dev&quot;,&quot;gen_prod&quot;,&quot;gen_dev&quot;,
&quot;gcc_prod&quot;,&quot;gcc_dev&quot;,&quot;jam_prod&quot;,&quot;jam_dev&quot;,&quot;jef_prod&quot;,&quot;jef_dev&quot;,&quot;mon_prod&quot;,&quot;mon_dev&quot;,&quot;one_prod&quot;,&quot;one_dev&quot;,&quot;pot_prod&quot;,
&quot;pot_dev&quot;,&quot;pur_prod&quot;,&quot;pur_dev&quot;,&quot;rcc_prod&quot;,&quot;rcc_dev&quot;,&quot;sth_prod&quot;,&quot;sth_dev&quot;,&quot;sul_prod&quot;,&quot;sul_dev&quot;,&quot;uls_prod&quot;,&quot;uls_dev&quot;,
&quot;exl_dev&quot;);

foreach $item (@results) {
$job = substr($item,-9,9);
push(@runningJobs,$job);
}

&checkJobs(@runningJobs);

sub checkJobs() {
my (@rJobs) = @_;
foreach $job (@testJobs) {
unless(grep /$job/, @rJobs ){
push (@stoppedJobs, $job);
}
}
}
}

 
h011ywood,PaulTEG
Thanks a lot for your help!!! I really appreciate it. I still have to add the functionality to email the stoppedJobs to the person in charge of these jobs.

 
Declare your array once, not redeclare for each item

Wuz al I waz sain'
--Paul
 
I keep getting this error:
Can't find string terminator &quot;EOF&quot; anywhere before EOF at detectJobs.pl line 48.
Code:
#!/usr/local/bin/perl
@results = `ps -ef | grep jobd`;
@testJobs = (&quot;aua_prod&quot;,&quot;aua_dev&quot;,&quot;alf_prod&quot;,&quot;alf_dev&quot;,&quot;bsc_prod&quot;,&quot;bsc_dev&quot;,&quot;cer_prod&quot;,&quot;cer_dev&quot;,&quot;cng_prod&quot;,&quot;cng_dev&quot;,&quot;can_prod&quot;,
&quot;can_dev&quot;,&quot;del_prod&quot;,&quot;del_dev&quot;,&quot;eri_prod&quot;,&quot;eri_dev&quot;,&quot;fin_prod&quot;,&quot;fin_dev&quot;,&quot;fre_prod&quot;,&quot;fre_dev&quot;,&quot;gen_prod&quot;,&quot;gen_dev&quot;,
&quot;gcc_prod&quot;,&quot;gcc_dev&quot;,&quot;jam_prod&quot;,&quot;jam_dev&quot;,&quot;jef_prod&quot;,&quot;jef_dev&quot;,&quot;mon_prod&quot;,&quot;mon_dev&quot;,&quot;one_prod&quot;,&quot;one_dev&quot;,&quot;pot_prod&quot;,
&quot;pot_dev&quot;,&quot;pur_prod&quot;,&quot;pur_dev&quot;,&quot;rcc_prod&quot;,&quot;rcc_dev&quot;,&quot;sth_prod&quot;,&quot;sth_dev&quot;,&quot;sul_prod&quot;,&quot;sul_dev&quot;,&quot;uls_prod&quot;,&quot;uls_dev&quot;,
&quot;exl_dev&quot;,&quot;ksb_dev&quot;);
#would probably be best to read these jobs from an external file, so it can be updated by an external interface
@stoppedJobs = ();
@runningJobs = ();
$mailprogram = &quot;/bin/mailx&quot;;
$sendto = &quot;ksbrace@hotmail.com&quot;;
$from = &quot;detectJobs.pl_script&quot;;
$subject = &quot;Jobs are not running&quot;;

foreach $item (@results) {

  $job = substr($item,-9,9);      #the substr will be quicker than split
  $job =~ s/^\s+(.*?)\s+$/$1/;    #
  $job =~ s/\s//g;                #
  push(@runningJobs,$job);        #


}
&checkJobs(@runningJobs);
$numStoppedJobs = @stoppedJobs;
if(numStoppedJobs > 0 ) {
  sendMail();
print &quot;@stoppedJobs \n&quot;;

sub checkJobs{
   my (@rJobs) = @_;
   foreach $job (@testJobs){
      unless( grep /$job/, @rJobs ){
         push @stoppedJobs, $job;
      }
   }
}

sub sendMail(){
  #-t option takes the headersfrom the lines following the mail
  #command -oi options prevent a period at the beginning of a line
  # from meaning end of input.
  open(MAIL, &quot;|$mailprogram -t -oi&quot;) || die &quot;Can't open mail program: $!\n&quot;;
  print MAIL &quot;To: $sendto\n&quot;;
  print MAIL &quot;From: $from\n&quot;;
  print MAIL &quot;Subject: $subject\n\n&quot;;
print MAIL <<EOF; #start a &quot;here document&quot;
Code:
        The following are jobs that didn't start:
        @stoppedJobs;
        EOF
  close MAIL;
  
  }

 
Code:
#!/usr/local/bin/perl
@results = `ps -ef | grep jobd`;
@testJobs = (&quot;aua_prod&quot;,&quot;aua_dev&quot;,&quot;alf_prod&quot;,&quot;alf_dev&quot;,&quot;bsc_prod&quot;,&quot;bsc_dev&quot;,&quot;cer_prod&quot;,&quot;cer_dev&quot;,&quot;cng_prod&quot;,&quot;cng_dev&quot;,&quot;can_prod&quot;,
&quot;can_dev&quot;,&quot;del_prod&quot;,&quot;del_dev&quot;,&quot;eri_prod&quot;,&quot;eri_dev&quot;,&quot;fin_prod&quot;,&quot;fin_dev&quot;,&quot;fre_prod&quot;,&quot;fre_dev&quot;,&quot;gen_prod&quot;,&quot;gen_dev&quot;,
&quot;gcc_prod&quot;,&quot;gcc_dev&quot;,&quot;jam_prod&quot;,&quot;jam_dev&quot;,&quot;jef_prod&quot;,&quot;jef_dev&quot;,&quot;mon_prod&quot;,&quot;mon_dev&quot;,&quot;one_prod&quot;,&quot;one_dev&quot;,&quot;pot_prod&quot;,
&quot;pot_dev&quot;,&quot;pur_prod&quot;,&quot;pur_dev&quot;,&quot;rcc_prod&quot;,&quot;rcc_dev&quot;,&quot;sth_prod&quot;,&quot;sth_dev&quot;,&quot;sul_prod&quot;,&quot;sul_dev&quot;,&quot;uls_prod&quot;,&quot;uls_dev&quot;,
&quot;exl_dev&quot;,&quot;ksb_dev&quot;);
#would probably be best to read these jobs from an external file, so it can be updated by an external interface
@stoppedJobs = ();
@runningJobs = ();
$mailprogram = &quot;/bin/mailx&quot;;
$sendto = &quot;ksbrace@hotmail.com&quot;;
$from = &quot;detectJobs.pl_script&quot;;
$subject = &quot;Jobs are not running&quot;;

foreach $item (@results) {

  $job = substr($item,-9,9);      #the substr will be quicker than split
  $job =~ s/^\s+(.*?)\s+$/$1/;    #
  $job =~ s/\s//g;                #
  push(@runningJobs,$job);        #


}
&checkJobs(@runningJobs);
$numStoppedJobs = @stoppedJobs;
if(numStoppedJobs > 0 ) {
  sendMail();
  print &quot;@stoppedJobs \n&quot;;
} #add this bracket

sub checkJobs{
   my (@rJobs) = @_;
   foreach $job (@testJobs){
      unless( grep /$job/, @rJobs ){
         push @stoppedJobs, $job;
      }
   }
}

sub sendMail() {
  #-t option takes the headersfrom the lines following the mail
  #command -oi options prevent a period at the beginning of a line
  # from meaning end of input.
  open(MAIL, &quot;|$mailprogram -t -oi&quot;) || die &quot;Can't open mail program: $!\n&quot;;
  print MAIL &quot;To: $sendto\n&quot;;
  print MAIL &quot;From: $from\n&quot;;
  print MAIL &quot;Subject: $subject\n\n&quot;;

        print MAIL <<&quot;EOF&quot;; #use this one
#        print MAIL <<EOF;   #start a &quot;here document&quot;
        The following are jobs that didn't start:
        @stoppedJobs;
EOF
  close MAIL;
  
  }

There's a missing right bracket from your if statement, as well as the syntax
Code:
print MAIL <<&quot;EOF&quot;;

HTH
--Paul
 
Not sure what the problem is. I threw the print <<EOF into a piece of code, at the top. I ran it once, and it printed. I ran it again, and again, and now nothing prints, with the same error. You can always surround the code with &quot;&quot;'s. Its a solution.

 
ok, I got it to work, only to find out that sendmail isn't set up. I was told to switch to mailx.
Code:
#!/usr/local/bin/perl
@results = `ps -ef | grep jobd`;
@testJobs = (&quot;aua_prod&quot;,&quot;aua_dev&quot;,&quot;alf_prod&quot;,&quot;alf_dev&quot;,&quot;bsc_prod&quot;,&quot;bsc_dev&quot;,&quot;cer_prod&quot;,&quot;cer_dev&quot;,&quot;cng_prod&quot;,&quot;cng_dev&quot;,&quot;can_prod&quot;,
&quot;can_dev&quot;,&quot;del_prod&quot;,&quot;del_dev&quot;,&quot;eri_prod&quot;,&quot;eri_dev&quot;,&quot;fin_prod&quot;,&quot;fin_dev&quot;,&quot;fre_prod&quot;,&quot;fre_dev&quot;,&quot;gen_prod&quot;,&quot;gen_dev&quot;,
&quot;gcc_prod&quot;,&quot;gcc_dev&quot;,&quot;jam_prod&quot;,&quot;jam_dev&quot;,&quot;jef_prod&quot;,&quot;jef_dev&quot;,&quot;mon_prod&quot;,&quot;mon_dev&quot;,&quot;one_prod&quot;,&quot;one_dev&quot;,&quot;pot_prod&quot;,
&quot;pot_dev&quot;,&quot;pur_prod&quot;,&quot;pur_dev&quot;,&quot;rcc_prod&quot;,&quot;rcc_dev&quot;,&quot;sth_prod&quot;,&quot;sth_dev&quot;,&quot;sul_prod&quot;,&quot;sul_dev&quot;,&quot;uls_prod&quot;,&quot;uls_dev&quot;,
&quot;exl_dev&quot;,&quot;ksb_dev&quot;);
#would probably be best to read these jobs from an external file, so it can be updated by an external interface
@stoppedJobs = ();
@runningJobs = ();
$mailprogram = &quot;/usr/lib/sendmail&quot;;
$sendto = &quot;kelly.brace@itec.mail.suny.edu&quot;;
$from = &quot;kelly.brace@itec.mail.suny.edu&quot;;
$subject = &quot;Jobs are not running&quot;;

foreach $item (@results) {

  $job = substr($item,-9,9);      #the substr will be quicker than split
  $job =~ s/^\s+(.*?)\s+$/$1/;    #
  $job =~ s/\s//g;                #
  push(@runningJobs,$job);        #


}
&checkJobs(@runningJobs);
$numStoppedJobs = @stoppedJobs;

if($numStoppedJobs > 0 ) {

  sendMail();
  print &quot;sent mail&quot;;
}
print &quot;@stoppedJobs \n&quot;;

sub checkJobs{
   my (@rJobs) = @_;
   foreach $job (@testJobs){
      unless( grep /$job/, @rJobs ){
         push @stoppedJobs, $job;
      }
   }
}

sub sendMail(){
  #-t option takes the headers from the lines following the mail
  #command -oi options prevent a period at the beginning of a line
  # from meaning end of input.
  open(MAIL, &quot;|$mailprogram -t -oi&quot;) || die &quot;Can't open mail program: $!\n&quot;;
  print MAIL &quot;To: $sendto\n&quot;;
  print MAIL &quot;From: $from\n&quot;;
  print MAIL &quot;Subject: $subject\n\n&quot;;
  print MAIL <<EOF;
        &quot;The following are jobs that didn't start:
@stoppedJobs;
EOF
close(MAIL);
  }

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top