1 Reply Latest reply on Jul 21, 2015 1:13 PM by Santhosh Kurimilla

    How to ensure shell/perl script return code is recognized by script job

    michael huttner

      We had a problem initially, getting an NSH script job to properly detect and return the exit status code from a shell/perl script, and I think I got it working doing something like this.  Does this make sense, and would you suggest an alternative?  It's a type 2 script job, using a parameter argument for the job code so we can test it (default is "2").

       

      echo Executing on $(hostname) at $(date)

      /bladelogic/depot/shared/tmp/myscript.pl $1 $2 $3 $4 $5 $6 $7 $8 $9 >&2

      EXIT_CODE=$?

      echo JOB exit code = $EXIT_CODE

      exit $EXIT_CODE

       

      Perl script:

      #!/usr/bin/perl

      my $code = shift || 0;

      print "Code argument: $code\n";

      print "This is running script:$0\n";

      print "Script is exiting with return code: $code\n";

      exit $code;

       

      From NSH/shell it works:

      $  ./testing.pl 0 && echo GOOD:$? || echo BAD:$?

      Code argument: 0

      This is running script: testing.pl

      Script is exiting with return code: 0

      GOOD:0

       

      $  ./testing.pl 2 && echo GOOD:$? || echo BAD:$?

      Code argument: 2

      This is running script: testing.pl

      Script is exiting with return code: 2

      BAD:2

       

      NSH job log:

      InfoJul 21, 2015 1:34:15 PMStarted running the job 'Testing' with priority 'NORMAL' on application server 'server'()
      InfoJul 21, 2015 1:34:17 PMExecuting work item NSH Script Job:Testing;  on application server: server.misc.com
      InfoJul 21, 2015 1:34:18 PMExecuting on server.misc.com at Tue Jul 21 12:34:18 CDT 2015
      InfoJul 21, 2015 1:34:18 PM-rwxrwxrwx   1 bladmin  bladmin       179 Jul 21 12:28 ...testing.pl
      InfoJul 21, 2015 1:34:18 PMExecuting CMD ...testing.pl 2 %h lkcma60489    
      ErrorJul 21, 2015 1:34:18 PMCode argument: 2
      ErrorJul 21, 2015 1:34:18 PMThis is running script:...testing.pl
      ErrorJul 21, 2015 1:34:18 PMScript is exiting with return code: 2
      InfoJul 21, 2015 1:34:18 PMThis Testing NSH script job reports exit code = 2
      InfoJul 21, 2015 1:34:18 PMExit Code 2
      ErrorJul 21, 2015 1:34:19 PMThe job 'Testing' has failed