Explore Courses Blog Tutorials Interview Questions
+4 votes
in DevOps and Agile by (29.3k points)

I am using Jenkins 2.46.1 I have a build pipeline plugin installed and I want to execute a Windows batch file in it. The batch file should execute in a new command window and not on Jenkins console output. I give the below Jenkins pipeline groovy script:

node {  

    stage 'Init'

    bat '''

        call C:\\myprj\\mybat.bat stop

        EXIT /B 0


    stage 'Deploy'

    bat '''call C:\\myprj\\mybat.bat'''


In the init stage, I want to kill the process if it is already open and in stage deploy it should open a new command window and run my batch file. The problem is that the above does not work. The build is successful but no command window opens up. Please suggest.

4 Answers

+5 votes
by (50.2k points)

To launch a new command window (cmd.exe) and run the batch file given.

You can use 

bat 'start cmd.exe /c C:\\myprj\\mybat.bat'

This command will do what you are asking that should be able to run.

Note: It depends on the working of your jenkins slave.

If you wish, you can learn more about Jenkins on Jenkins Tutorial and Jenkins Certification.

by (29.3k points)
Can I run the Bat file from its original location?
by (50.2k points)
No you cannot run the bat file from it's original location. If you try to run from a temp location  created by durable task system.Your script might not return the path.
by (19.7k points)
It helped, thanks!
by (19k points)
It worked for me.
by (33.1k points)
Thanks, Your answer worked well.
by (47.2k points)
The reason why you must open in a new command window is that the batch file will run a server. The command window remains open as long as the server is running. So the build will always show as 'getting executed' and never as 'completed
by (29.5k points)
nice solution worked for me!!!
by (40.7k points)
Good explanation. Thank you.
+1 vote
by (108k points)

Just replace cmd \k with call

The Execute Windows Batch command is running inside a cmd already so no need to specifically start a new one. Just "call" your BAT file.

by (44.4k points)
why in some jobs we can use CMD to run, but in some jobs, it cannot?
0 votes
by (19.9k points)

Create file name jenkins.bat and place this file where your jenkins.war file is available.

You have to click jenkins.bat file.

Note :- After click on jenkins.bat file, chrome browser will automatically open with jenkins url.

Try this If your system configured with Java 8 or 7 Installation.

timeout 3

start chrome http://localhost:8080

timeout 3

java -jar jenkins.war

Try this If your system configured with Java 10 or above Installation.

timeout 3

start chrome http://localhost:8080

timeout 3

java -jar jenkins.war --enable-future-java

0 votes
by (106k points)

You can try like this,  first point it to the path where your bat file exists, and then 'call' command to trigger that bat file. As below:

cd yourFilePath

and then

call yourFile.bat

Browse Categories