Error running Windows batch file via build step

14,205 views
Skip to first unread message

Lee Winder

unread,
Apr 19, 2012, 7:43:30 AM4/19/12
to jenkins...@googlegroups.com
I have a single batch file which calls a number of executables which builds various assets for our application.  When I run the batch file manually it works perfectly (the command prompt opens, the assets are built - takes about 10 minutes - and then it closes).

If I run this batch file via a build step, it successfully calls the batch file, but the individual steps inside the batch file fail (it just skips over each one).  I'm assuming this is down to some permission issue between the user used when I run the file manually and the user used when Jenkins runs it.

If I move the content of the batch file into the build step itself I get the same results.

In the System Information screen my user name is actually my computer name (with a $ on the end) rather than an explicit user as defined in my system admin.

What user is used to run build steps and how can I change that user?  Has anyone else experienced this problem before and has a solution?

Thanks
Lee

Jan Seidel

unread,
Apr 19, 2012, 7:50:09 AM4/19/12
to Jenkins Users
Hi Lee,

some more information would be nice. I did forget my crystal orb at
home ;)
Which OS are you using?
Do you run Jenkins as service?
A snippet of your batch files would be helpful as much as an outtake
of your build log would be.

Take care
Jan

Lee Winder

unread,
Apr 19, 2012, 8:08:27 AM4/19/12
to jenkins...@googlegroups.com

Hi Jan, sorry, should have added this stuff but I was hoping the user permission issue might have been a known problem :)

 

OS is Win7, Jenkins is running as a standalone service (default service as set up by the Jenkins .msi installer -  http://jenkins-ci.org/content/windows-installers-are-now-available)

 

Snap shot of the batch files is a tad harder to show as we're working under NDA, but it generally looks like

 

cd "C:\Project\Asset_1\Export"

C:\Project\Repository\Builder.exe     --a_lot_of_options_for_assets_1

 

cd "C:\Project\Asset_2\Export"

C:\Project\Repository\Builder.exe     --a_lot_of_options_for_assets_2

 

Console output is simply the content of the batch file followed by failure

 

Started by user Lee Winder

Building in workspace C:\Program Files (x86)\Jenkins\jobs\02 - Daily Build\workspace

[workspace] $ cmd /c call C:\Windows\TEMP\hudson9141842775998444888.bat

 

C:\Program Files (x86)\Jenkins\jobs\02 - Daily Build\workspace>cd "C:\Project\Asset_1\Export"

C:\Project\Asset_1\Export>C:\Project\Repository\Builder.exe     --a_lot_of_options_for_assets_1

 

C:\Program Files (x86)\Jenkins\jobs\02 - Daily Build\workspace>cd "C:\Project\Asset_2\Export"

C:\Project\Asset_2\Export>C:\Project\Repository\Builder.exe     --a_lot_of_options_for_assets_2



Build step 'Execute Windows batch command' marked build as failure

Sending e-mails to: ...

Finished: FAILURE

 

The build step simply looks like

C:\Project\Build_Scripts\Build_All_Assets.bat


If I drop the commands into the Build Step directly I get the following failure message


C:\Project\Asset_2\Export>exit 255

Build step 'Execute Windows batch command' marked build as failure

Sending e-mails to: ...

Finished: FAILURE

I'm currently investigating the Builder.exe tool for more information but this is a legacy project and we don't have the source code for all parts of the process which makes it quite difficult!

Thanks for your help
Lee

Jan Seidel

unread,
Apr 19, 2012, 8:20:41 AM4/19/12
to Jenkins Users
What irritates me a bit is the exit code 255.
Thats a not quite usual one from a plain build step. Looks to me like
a return value from your builder.

The builder runs for sure? also when you build it in the workspace C:
\Program Files (x86)\Jenkins\jobs\02 - Daily
Build\workspace ?
I would always try to avoid paths with spaces or in this case even
brackets. So I would test 2 things upfront.
What happens when you execute the builder.exe with the "runas"
command?
What happens if at least the workspace is locate somewhere with plain
numbers and letters only?

To execute from a build step especially if one file calls another
action may lead to an anonymous user latest for the action/second file
call (if embedded).
So calling a btach file that calls the builder.exe would not be my
first choice ;)

Take care
Jan

Jan Seidel

unread,
Apr 19, 2012, 8:34:11 AM4/19/12
to Jenkins Users
By the way.

It looks like some of your steps finish successfully right?
C:\Project\Asset_2\Export>C:\Project\Repository\Builder.exe --
a_lot_of_options_for_assets_2 is just failing if I dont get it wrong

Maybe you should prepend at each call
C:\Project\Asset_1\Export>C:\Project\Repository\Builder.exe --
a_lot_of_options_for_assets_1
IF %ERRORLEVEL% NEQ 0 echo ##### Builder.exe --
a_lot_of_options_for_assets_1 failed ##### && exit /B 1

C:\Project\Asset_2\Export>C:\Project\Repository\Builder.exe --
a_lot_of_options_for_assets_2
IF %ERRORLEVEL% NEQ 0 echo ##### Builder.exe --
a_lot_of_options_for_assets_2 ##### && exit /B 2


You can use the echo and/or exit to exactly know where it crashes. The
echo may not work if the text is too long or using echo-critical
characters.
Exit /B [call_number] is less prone to failure and you have simply to
count your way through the steps ;)

Lee Winder

unread,
Apr 19, 2012, 10:56:20 AM4/19/12
to jenkins...@googlegroups.com
Hi Jan

I'm unsure if the first step succeeds, as if I just run a batch script with the same entry it fails for the same reason.

But you've given me a number of things to try here, so I'll give them a try and see what the results are.

Just a quick question, I assume when running the job that Jenkins does use a different user than the one logged in?  Is that correct, and is it possible to change that user?

Thanks

Jan Seidel

unread,
Apr 20, 2012, 3:48:12 AM4/20/12
to Jenkins Users
Hi Lee,

afaik is the logged in user who triggers the job providing the
context. It does not matter who is logged on in case the job is
triggered via the cron job of Jenkins. The service account is then
used at all times.
You could amend the jenkins service credentials to a expicit user but
I wouldn't mess around there. I've been there, didn't like it and went
away ;)

You can apply credentials of subsequent program/command calls with
help of the runas command.
Easiest is of course if all steps are running in Jenkins context to
make troubleshooting easier. But that's not always possible.
e.g. call an URL with wget. This will always end up in an anonymous
user if you don't apply some credentials.

Take care
Jan

Indyn

unread,
May 16, 2012, 12:27:06 PM5/16/12
to Jenkins Users
Hello Lee and Jan,

I am having a similar problem. My batch script was simply:
-----
cd "C:\Installs\Win32Forth 6.14.00"
start-both-RTests.exe
-----
When I ran the job, the Console Output was:
--------
Started by user anonymous
Building on master in workspace C:\Program Files (x86)\Jenkins\jobs
\Client Code1\workspace
[workspace] $ cmd /c call C:\Windows\TEMP
\hudson5297019636393332101.bat

C:\Program Files (x86)\Jenkins\jobs\Client
Code1\workspace>ClientCode1.bat

C:\Program Files (x86)\Jenkins\jobs\Client Code1\workspace>cd "C:
\Installs\Win32Forth 6.14.00"

C:\Installs\Win32Forth 6.14.00>start-both-RTests.exe
--------
Jenkins was hung (swirling symbol, flashing red dot beside Console
Output) at the last line.

I tried the runas, but it required a password to be entered
interactively. I don't see how to do the runas in a script.

Lee, did you get your exe to run? Any more hints or suggestions?

Thanks,
Indyn

On Apr 20, 1:48 am, Jan Seidel <wakkal...@gmail.com> wrote:
> Hi Lee,
>
> afaik is the logged in user who triggers the job providing the
> context. It does not matter who is logged on in case the job is
> triggered via the cron job of Jenkins. The service account is then
> used at all times.
> You could amend the jenkins service credentials to a expicit user but
> I wouldn't mess around there. I've been there, didn't like it and went
> away ;)
>
> You can apply credentials of subsequent program/command calls with
> help of the runas command.
> Easiest is of course if all steps are running in Jenkins context to
> make troubleshooting easier. But that's not always possible.
> e.g. call an URL with wget. This will always end up in an anonymous
> user if you don't apply some credentials.
>
> Take care
> Jan
>
> On 19 Apr., 16:56, Lee Winder <lee.win...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hi Jan
>
> > I'm unsure if the first step succeeds, as if I just run abatchscript with
> > > > > Snap shot of thebatchfiles is a tad harder to show as we're working
> > > under
> > > > > NDA, but it generally looks like
>
> > > > > cd "C:\Project\Asset_1\Export"
>
> > > > > C:\Project\Repository\Builder.exe     --a_lot_of_options_for_assets_1
>
> > > > > cd "C:\Project\Asset_2\Export"
>
> > > > > C:\Project\Repository\Builder.exe     --a_lot_of_options_for_assets_2
>
> > > > > Console output is simply the content of thebatchfile followed by
> > > failure
>
> > > > > Started by user Lee Winder
>
> > > > > Building in workspace C:\Program Files (x86)\Jenkins\jobs\02 - Daily
> > > > > Build\workspace
>
> > > > > [workspace] $ cmd /c call
> > > C:\Windows\TEMP\hudson9141842775998444888.bat
>
> > > > > C:\Program Files (x86)\Jenkins\jobs\02 - Daily Build\workspace>cd
> > > > > "C:\Project\Asset_1\Export"
>
> > > > > C:\Project\Asset_1\Export>C:\Project\Repository\Builder.exe
> > > > > --a_lot_of_options_for_assets_1
>
> > > > > C:\Program Files (x86)\Jenkins\jobs\02 - Daily Build\workspace>cd
> > > > > "C:\Project\Asset_2\Export"
>
> > > > > C:\Project\Asset_2\Export>C:\Project\Repository\Builder.exe
> > > > > --a_lot_of_options_for_assets_2
>
> > > > >  Build step 'ExecuteWindowsbatchcommand' marked build as failure
>
> > > > > Sending e-mails to: ...
>
> > > > > Finished: FAILURE
>
> > > > > The build step simply looks like
>
> > > > > C:\Project\Build_Scripts\Build_All_Assets.bat
>
> > > > >  If I drop the commands into the Build Step directly I get the
> > > following
> > > > > failure message
>
> > > > > C:\Project\Asset_2\Export>exit 255
>
> > > > > Build step 'ExecuteWindowsbatchcommand' marked build as failure
>
> > > > > Sending e-mails to: ...
>
> > > > > Finished: FAILURE
>
> > > > > I'm currently investigating the Builder.exe tool for more information
> > > but
> > > > > this is a legacy project and we don't have the source code for all
> > > parts of
> > > > > the process which makes it quite difficult!
>
> > > > > Thanks for your help
> > > > > Lee
>
> > > > > On Thursday, 19 April 2012 12:50:09 UTC+1, Jan Seidel wrote:
>
> > > > > > Hi Lee,
>
> > > > > > some more information would be nice. I did forget my crystal orb at
> > > > > > home ;)
> > > > > > Which OS are you using?
> > > > > > Do you run Jenkins as service?
> > > > > > A snippet of yourbatchfiles would be helpful as much as an outtake
> > > > > > of your build log would be.
>
> > > > > > Take care
> > > > > > Jan
>
> > > > > > On 19 Apr., 13:43, Lee Winder <lee.win...@gmail.com> wrote:
> > > > > > > I have a singlebatchfile which calls a number of executables
> > > which
> > > > > > builds
> > > > > > > various assets for our application.  When I run thebatchfile
> > > manually
> > > > > > it
> > > > > > > works perfectly (the command prompt opens, the assets are built -
> > > takes
> > > > > > > about 10 minutes - and then it closes).
>
> > > > > > > If I run thisbatchfile via a build step, it successfully calls
> > > the
> > > > > >batch
> > > > > > > file, but the individual steps inside thebatchfile fail (it just
> > > skips
> > > > > > > over each one).  I'm assuming this is down to some permission
> > > issue
> > > > > > between
> > > > > > > the user used when I run the file manually and the user used when
> > > > > > Jenkins
> > > > > > > runs it.
>
> > > > > > > If I move the content of thebatchfile into the build step itself

Lee Winder

unread,
May 17, 2012, 4:36:43 AM5/17/12
to jenkins...@googlegroups.com
Hi Indyn

I solved my problem by moving the executable calls outside of the build process.  I now have a few scripts constantly running in the background, which the build process sends a message to when they want the executables to run, and then waits for the executable scripts to flag back to the build system that they have finished running and that it can continue.  The build system step for that is simply a small script which sends a message and waits for receipt of another one, nothing more complicated than that.

It's not an ideal solution as it takes the error reporting and console output out of the build system so its more difficult to monitor, and this permission error has happened a couple of times now so I have a number of this constantly running scripts in the background.  Any more and it'll become difficult to maintain.

This is my first experience of Jenkins, so I'm putting the issue down to my lack of understanding in how the system works, but I don't think I'll continue to use the system if this happens in the same way on future projects.

Jan Seidel

unread,
May 23, 2012, 4:21:59 AM5/23/12
to jenkins...@googlegroups.com
Hi folks,

runas has an option called  /savecred. You would have to use runas once in the wanted user context and enter the credentials interactively.
After that should it also work non-interactive.

HTH
Jan

Christopher Parker

unread,
Dec 19, 2012, 4:39:50 AM12/19/12
to jenkins...@googlegroups.com
This happened to me as well...but I fixed it using this post:

http://stackoverflow.com/questions/11554324/batch-file-errorlevel-issue

Rahul Kulkarni

unread,
Dec 22, 2016, 4:37:16 AM12/22/16
to Jenkins Users
Hi Lee,
I am not sure if you are active in this group still but just giving a try. Can you please explain in detail what u did exactly?

Prashant Singhal

unread,
Jan 10, 2018, 1:16:19 AM1/10/18
to Jenkins Users
Hi,

I too faced same problem. 
I tried runas .
Also tried with variuos options of CMD in x32 and x64.

I am nost sure about actual casuse but I observed that My Jenkins was installed in x32 
C:\Program Files (x86)\Jenkins\jobs\....
> > > > > Build\workspace 
> > > C:\Windows\TEMP\jenkins2333988544009686929.bat

So I re-configured my Jenkins in x64 and the problem got resolved.
I believe the *.bat file in TEMP uses the process as per Jenkins CPU x32 and this may be the reason.
The program I tried to run via batch Command was an x64 program . 

I still need to investigate on how the TEMP\*.bat process works in Jenkins. Any help will be appreciated. Thanks.


On Thursday, May 17, 2012 at 2:06:43 PM UTC+5:30, Lee Winder wrote:

Sai Krishna

unread,
Feb 14, 2018, 8:40:23 AM2/14/18
to Jenkins Users
Hi all,
I too faced same kind of issue. Using Jenkins, i trying to do deploy the war artifact in tomcat manually. To do that, instead of using plugin, wrote script in "windows batch command " but i getting below error.

BUILD SUCCESSFUL
Total time: 5 seconds
[Ant-tomcat] $ cmd /c call C:\WINDOWS\TEMP\hudson4425405522418561213.bat

C:\Program Files (x86)\Jenkins\workspace\Ant-tomcat>echo "Starting to copy the build" 
"Starting to copy the build"

C:\Program Files (x86)\Jenkins\workspace\Ant-tomcat>copy C:\Program Files (x86)\Jenkins\workspace\Ant-tomcat\war\SampleAntProject.war D:\Devops_3rd\apache-tomcat-8.5.24\webapps\ 
The system cannot find the file specified.

C:\Program Files (x86)\Jenkins\workspace\Ant-tomcat>echo "Copied the build to tomcat" 
"Copied the build to tomcat"

C:\Program Files (x86)\Jenkins\workspace\Ant-tomcat>exit 1 
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE

Kindly help me to solve this issue.
then i tried the same manually in command prompt using copy command but no use.

D:\>copy C:\Program Files \(x86)\Jenkins\workspace\Ant-tomcat\war\SampleAntProject.war D:\Devops_3rd\apache-tomcat-8.5.24\webapps\
The system cannot find the file specified.

anyone has a solution for this?
Reply all
Reply to author
Forward
0 new messages