3

I have following code in my script.

echo Trying to kill all node processes.
taskkill /f /im node.exe

echo Running the application...
start npm run prod

echo Success...

The script runs fine if I open a command prompt and run it from there but it doesn't start the npm run process when I run it from Jenkins pipeline.

Strange thing is the build gets success.

Can anyone help me solve this riddle? Thanks. Update - 1 This is the output in Jenkins.

up to date in 23.58s
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deployment)
[Pipeline] bat
[ABC Pipeline] Running batch script
*************************************
    Build Started
    @author: 
*************************************
Trying to kill all node processes.
ERROR: The process "node.exe" not found.
Running the application...
Success...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Automation Testing)
[Pipeline] echo
Testing...
[Pipeline] echo
Tests passed!
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Update - 2 The npm file extension is .cmd and Windows 10 with Jenkins 2.89.4

Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
  • What is the error log you are getting? – Gokul Nath KP Feb 28 '18 at 08:37
  • No error is there. Only every message is being printed properly but some how start isn't working. – Bilbo Baggins Feb 28 '18 at 09:14
  • I have done similar thing, by trying to start calculator. I also didn't notice that the process was started, until I have put start-sleep 5 after I start process. Process is indeed started, but stopped after Jenkins finishes. Trying to figure out why is that. – Vladimir Bundalo Feb 28 '18 at 09:38
  • Yeah I think similar thing is happening here. I noticed that there are several command prompts starting when the job executes but then they are terminated once the build is over. – Bilbo Baggins Feb 28 '18 at 09:48
  • @VladimirBundalo I have created a jenkins issue for this. https://issues.jenkins-ci.org/browse/JENKINS-49790 – Bilbo Baggins Feb 28 '18 at 10:10
  • `npm` is not an executable. It is a batch file. So use `call npm run prod`, best with full qualified name of `npm` batch file, i.e. path + name + extension. See also [this answer](https://stackoverflow.com/a/24725044/3074564) explaining 4 methods to run a batch file from within a batch file. I have never installed NPM package and no questioner ever posted the file extension of file with name `npm`. I would really like to know if `npm` is `npm.bat` or `npm.cmd`. @BilboBaggins Could you post with a comment which file extension the file `npm` has or edit your question and add the file extension? – Mofi Feb 28 '18 at 11:46
  • I would also suggest not using option `/f` to __kill__ `node.exe` by Windows and instead send `node.exe` to close event message so that it can __terminates itself gracefully__. Really killing a process by the operating system should be done only if the process does not respond on close/terminate signal and does not terminate itself within a few seconds. – Mofi Feb 28 '18 at 11:51
  • @Mofi I have updated my question, it is a .bat file. Can you please suggest/explain what you just said about not killing the node. How can I send close event message to node.exe from command line ? – Bilbo Baggins Feb 28 '18 at 13:29
  • @BilboBaggins Many thanks for posting that `npm` is in real `npm.cmd` (according to information in question). Now I know the real file name finally. For `taskkill /im node.exe` without `/f` versus `taskkill /f /im node.exe` see for example [this answer](https://stackoverflow.com/a/44817461/3074564) or [Close programs from the command line (Windows)](https://superuser.com/questions/727724/) on Super User. Please take also look on [What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?](https://stackoverflow.com/a/41461002/3074564) – Mofi Feb 28 '18 at 20:34

1 Answers1

2

Windows services are not able to open new console windows but START is doing exactly this. So if you are running Jenkins as a service, this won't work. However, your script should work fine if you simply skip the START command. Just npm run prod should be enough.

EDIT:

OK, here is a workaround: if you really need to use the start command, you could create a task with the task scheduler. Righ in there you can put any batch code you want or just point it to a bat file. Use Schtasks /run /TN TaskName to execute your task from Jenkins.

MichaelS
  • 5,941
  • 6
  • 31
  • 46