How do I retrieve the exit code of a 'bat' command?

29 views
Skip to first unread message

Scott Richmond

unread,
Apr 14, 2016, 5:07:27 PM4/14/16
to Jenkins Users
I need to run a few external build tools and I do so with the bat command on Windows agents. I need to be able to determine the success or failure of that command based on the exit code of the script that was run. Is it possible to get this?

Richard Bywater

unread,
Apr 14, 2016, 5:26:08 PM4/14/16
to Jenkins Users
Someone may have a more elegant answer but one way that comes to mind is to call another cmd/bat file from Jenkins which then calls the external tools and also has logic to interpret the exit codes coming back from the those tools and acting appropriately.

Richard.

On Fri, 15 Apr 2016 at 09:07 Scott Richmond <s.t.ri...@gmail.com> wrote:
I need to run a few external build tools and I do so with the bat command on Windows agents. I need to be able to determine the success or failure of that command based on the exit code of the script that was run. Is it possible to get this?

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/b1f502b2-e559-49d9-a9b3-7d33f4fa89ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scott Richmond

unread,
Apr 14, 2016, 5:40:36 PM4/14/16
to Jenkins Users
So I've done some testing - One can catch a bad exit from bat with a trycatch function. HOWEVER! It does not seem to catch exit code 1. It'll catch other higher numbered exit codes, but not 1. Not sure what to do here.

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/Dr1u9PVUvVw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAMui947b10_P7J_-GiEXAVUwNZGPSYGvsGGeHpZ2%2BdXWOOUZJw%40mail.gmail.com.

Richard Bywater

unread,
Apr 14, 2016, 5:54:42 PM4/14/16
to Jenkins Users
You should just be able to use simple checks for the ERRORLEVEL variable to do some basic if blocks. See http://stackoverflow.com/questions/4451013/check-if-process-returns-0-with-batch-file for a basic example and also I think it has a link to more info.

Richard.

Scott Richmond

unread,
Apr 14, 2016, 6:02:08 PM4/14/16
to Jenkins Users
I had that thought too, however for whatever reason I cannot seem to get that env var in my script with echo "Error level is ${env.ERRORLEVEL}". It always prints null.

Victor Martinez

unread,
Apr 15, 2016, 3:16:55 AM4/15/16
to Jenkins Users
"Error level is ${env.ERRORLEVEL}" ??

Are you using any specific plugin rather than a batch build step? It seems, that env.ERRORLEVEL refers to the pipeline plugin, doesn't it?
Reply all
Reply to author
Forward
0 new messages