I have a problem in my jenkins job and I isolated into one command. So I created another separate job to try to fix it.
So in this job, called "teste" I only have one single command:
start cmd /k call "C:\Program Files\myDir\myBat.bat"
This opens a separate cmd window running my bat file, which should keep running "forever".
But the problem is when I do it, my jenkins job keeps stuck into a "exit 0" operation that I have no idea from where it came from.
Thats the console:
[EnvInject] - Loading node environment variables.
Building remotely on Machine01 in workspace C:\workspace\teste
[teste] $ cmd /c call C:\...dir\jenkins.bat
C:\workspace\teste>start cmd /k call "C:\Program Files\myDir\myBat.bat"
C:\workspace\teste>exit 0
Then it keep stuck at that point.
Example of myBat.bat content:
echo hi
pause
There's any way to make this call in another window without waiting for its finish?
I solve my problem changing the way I was calling my other .bat, calling it through powershell. But since I was from a bat file, I used the command to send a powershell command, calling my other bat file.
Also, I've added another line changing the jenkins BUILD_ID to a fake one, so it doesn't kill it.
So I changed from this line:
start cmd /k call "C:\Program Files\myDir\myBat.bat"
To this :
set BUILD_ID=dontKillMe
powershell -Command "Start-Process 'C:\Program Files\myDir\myBat.bat'"
I hope it helps someone someday.
Related
I have this file.bat :
cd "C:\Program Files(x86)\Anydesk" && anydesk.exe
If i double click on it it works fine and does what i want.
Now i try to launch this bat file inside of my C program :
system("path\\file.bat");
But it does nothing. I see a super fast cmd opening and nothing else.
I am wondering maybe it is failing because it is calling another application? But i am not sure.
How to make this work?
.bat is not an executable. It is a script which is processed by cmd.com.
So you need to execute it, with your .bat as a parameter:
system("cmd /C path\\script.bat");
The /C key will tell your cmd, to execute the bat and exit, once the bat is finished. You can use /K for debug purposes (execute and remain open after completion).
So I tried exit and I tried putting a 2 second delay before exit but neither worked. After the bat file successfully runs, the CMD window will stay on.
I did notice however that a CMD window pops up and disappears right away and then the empty CMD window just stays there. (It's like there are two CMD windows)
#echo off
cd C:\Program Files\obs-studio\bin\64bit
"C:\Program Files\obs-studio\bin\64bit\obs64.exe" --collection Replay, --profile Replay, --scene Scene, --startreplaybuffer --minimize-to-tray
exit
EDIT: Also removing the cd line doesn't make the batch file work. I'm not sure why.
The window doesn't close because calling an app directly will tell the batch execution to wait for the app to close before running the next line. Use Start instead. So your batch will be :
#echo off
cd C:\Program Files\obs-studio\bin\64bit
start "" "C:\Program Files\obs-studio\bin\64bit\obs64.exe" --collection Replay, --profile Replay, --scene Scene, --startreplaybuffer --minimize-to-tray
The exit is superfluous since after the end line it should close anyway. By the way if you're just looking to create something to click/call for launching OB studio with those parameters, using shortcut should be enough (put C:\Program Files\obs-studio\bin\64bit on Start In and write the whole command and parameters on Target)
Currently I want to run a batch file that fires the command git log and show me that log.
After that I need to be able to commit and view the status so this prompt may not disappear after a key press.
I've searched the net and the only answer people have is pause which close the prompt after a keypress.
Does anyone have the solution for me? Currently I made a shortcut to cmd.exe and made the target my folder, but I want to execute some commands also.
Thanks in advance.
This (below) tested OK in Windows 7. To exit the window it creates, type "exit" when done.
start cmd /K "cd \[the-target-folder] && git log"
Where:
[the-target-folder] you replace with your target folder
Note:
&& lets you run two commands on one line
/K is a parameter to the cmd shell program which which carries out the command specified by string and remains.
I want to ask you all how to run batch files sequentially in Windows.
I have tried :
start /w batchfile_1.bat
start /w batchfile_2.bat
..
start /w batchfile_n.bat
but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one.
Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?
Thanks a lot.
I would check the solutions to this question: Run Multiple batch files
Taken from the answer in the link.
Use call:
call bat1.cmd
call bat2.cmd
By default, when you just run a batch file from another one control will not pass back to the calling one. That's why you need to use call.
Basically, if you have a batch like this:
#echo off
echo Foo
batch2.cmd
echo Bar
then it will only output
Foo
If you write it like
#echo off
echo Foo
call batch2.cmd
echo Bar
however, it will output
Foo
Bar
because after batch2 terminates, program control is passed back to your original batch file.
If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.
#echo off
.
.
:: Inspired coding
.
.
exit
I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:
A START /W command is invoked and starts a batch file.
The batch file starts executing and runs a program.
The batch file finishes and its console window remains open, but the program continues running.
The START /W command that was used to run the batch file is still executing because the console window remains open.
You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.
Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.
I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.
This is sort of a follow-up to my question earlier (link).
To test things out I made this simple batch file to ensure the Task Scheduler was properly executing the batch file:
cd "C:\Users\user\Desktop"
echo. 2>test.txt
So after the test.txt document is created on the desktop, the batch file should end but it continues to run:
Is there a way, either at the end of the batch file or a setting in the Task's Properties, to ensure that the cmd process quits?
Thanks!
I ran into the exact same problem. However, I felt duped when I read what Trevor778 wrote in this post:
I had the same problem - the task worked but the status kept showing Running. One simple thing to try is click on the Task Scheduler Library in the left column. Click Action/Refresh. Presto. Status changed to Ready. That's all it was for me, the task ran fine, just the status didn't update. Hope this helps.
ref: https://social.technet.microsoft.com/Forums/en-US/2f6dc29c-3b8b-45f5-a2a7-53e076acc062/task-scheduler-scheduler-status-is-being-running-always?forum=winservergen
you can add "exit" to last line of your script
cd "C:\Users\user\Desktop"
echo. 2>test.txt
exit
Running TASKKILL /F /IM cmd.exe will kill all cmd.exe processes whether it was the one that spawned this batch file or not. That's probably not desirable behavior. :-)
Judging by your last question, I'm guessing you're still running your task with cmd.exe /k, which will keep that window open indefinitely. For an unattended task, cmd.exe /c is a better choice. When the batch file finishes, the process should end.
Same here on Windows 7.
Putting all batch files in a directory in the user User specific path who runs the task
run programm = " cmd.exe " (without a path)
Your extras, mine where = " /c "C:\Users[username]\whatever\your_batchfile.bat" >> log.txt" "
" >> log.txt " so that i can see the output of the batch...
start in = " C:\Users[username]\whatever "
I also checked the "run with highest privilges" box
after that everything worked fine :)
Use following
exit /B
you may find more information in windows console area then type:exit/?
I know it's an old question, but I personally found that if I let a pause at the end of the bat file, it would keep the status as "Running".
I usually leave a pause at the end to help with debugging, but I found when I removed it, the task scheduler finally recognised it as having exited. It didn't help if I just refreshed it.
The solution I found was to add this line at the very end of the batch file:
TASKKILL /F /IM cmd.exe
Now after the batch file task runs and completes, it is no longer in the All Running Tasks list and the status goes back to 'Ready' instead of staying at 'Running'.
Warning:
That command will kill all running command processor instances so it may be potentially harmful!