1

when I am running a Tcl script that contains the following lines:

set V [exec bjobs ]
puts "bjobs= ${V}"

When jobs are present it's working properly but, no jobs are running it is showing an error like this:

No unfinished job found
    while executing
"exec bjobs "
    invoked from within
"set V [exec bjobs ]"

How to avoid this error? Please let me know how to avoid this kind of errors.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215

3 Answers3

2

It sounds to me like the bjobs program has a non-zero exit code in this case. The exec manual page includes this example in a subsection WORKING WITH NON-ZERO RESULTS:

To execute a program that can return a non-zero result, you should wrap the call to exec in catch and check the contents of the -errorcode return option if you have an error:

      set status 0
      if {[catch {exec grep foo bar.txt} results options]} {
          set details [dict get $options -errorcode]
          if {[lindex $details 0] eq "CHILDSTATUS"} {
              set status [lindex $details 2]
          } else {
              # Some other error; regenerate it to let caller handle
              return -options $options -level 0 $results
          }
      }

This is more easily written using the try command, as that makes it simpler to trap specific types of errors. This is done using code like this:

      try {
          set results [exec grep foo bar.txt]
          set status 0
      } trap CHILDSTATUS {results options} {
          set status [lindex [dict get $options -errorcode] 2]
      }

I think you could write this as:

try {
    set V [exec bjobs ]
} trap CHILDSTATUS {message} {
    # Not sure how you want to handle the case where there's nothing...
    set V $message
}
puts "bjobs= ${V}"
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
0
if {[catch {exec bjobs} result]} {
    puts "bjobs have some issues. Reason : $result"
} else {
    puts "bjobs executed successfully. Result : $result"
}

Reference : catch

Dinesh
  • 16,014
  • 23
  • 80
  • 122
  • This code suppress error message, along with that I want to suppress the "No unfinished job found" message or redirect it to variable. can u please help me in doing this – INKULA GUNNESH Mar 13 '21 at 06:05
0

Note carefully in the exec man page:

If any of the commands in the pipeline exit abnormally or are killed or suspended, then exec will return an error [...]
If any of the commands writes to its standard error file and that standard error is not redirected and
-ignorestderr is not specified, then exec will return an error.

So if bjobs returns non-zero or prints to stderr when there are no jobs, exec needs catch or try as Donal writes.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352