Run command one by one in cmd for Windows - windows

I want to do following:
Start a dotnet application in new process
Run some commands one by one, the next one should wait when previous finished/closed
So my cmd file looks like:
START /B dotnet "D:\myapp\MyApp.dll" # don't know how to open in new process/window
START "" RunACommand.exe -p -t # this must be started immediately after executing above command
START "" RunBCommand.exe # this must be executed after RunACommand has finished
START "" RunCCommand.exe # this must be executed after RunBCommand has finished
How to achieve ?

You need to add /wait:
START /wait "" RunCCommand.exe # this must be executed after RunBCommand has
finished

Related

Make git-hook call cmd.exe without waiting for that process to finish

On a windows computer, I am adding a git-hook that starts a build; this needs to be run through cmd.exe, and I do not want the git-hook script to wait for that to finish executing, as that would mean each push would take a long time to finish.
To reproduce the problem, run these commands in an empty directory from git bash on windows:
mkdir githooks && cd githooks/
git init --bare
cd ..
git clone githooks clone
cd clone
git commit --allow-empty -m "1"
And save the following to ./githooks/hooks/update
#!/bin/bash
cmd.exe /c "sleep 2"
exit 1;
In the git bash window, from the clone directory I left you in, run git push. Note that ending the update script with exit 1; is convenient as you can re-run the git push after every edit to the git-hook script.
I have tried every combination I can think of for the two methods of "not waiting for the called script to exit"; in bash, you append an ampersand &:
cmd.exe /c "sleep 2" &
in cmd.exe (bat files), you would start the line:
cmd.exe /c "start \"title\" sleep 2"
I would expect either of these approached to work - i.e. the git push should fail quickly, rather than wait 2 seconds before failing.
Nesting cmd's does not seem to affect this:
cmd.exe /c "cmd.exe /c \"start \"\" sleep 2\"" &
When submitting an answer, be sure to also check that the command given to cmd.exe actually executes, by replacing sleep 2 with e.g. mkdir ..\foo so that you can check that, not only does the script finish quickly when given a sleep command, but when given a mkdir command, the directory is actually created.
cmd.exe /c "mkdir ..\foo"
For instance, the following finishes immediately, but that is because the sleep 2 never gets executed, as /c needs the next parameter to be quoted.
cmd.exe /c start \"title\" sleep 2
I was trying to do the same thing and eventually got an idea to call another bash script with:
sh $(pwd)/.git/hooks/runCmd.sh >/dev/null 2>&1 &
Which calls the CMD script I want and passes the argument.
Body of the bash script:
#!/bin/sh
start "Path\MyCmd.cmd" hook
exit 0  
Path in this case is relative to the repository root.

Executing windows batch command in Jenkins keeps failing with Exit-1 state

I am testing out "Jenkins ver. 2.89.4" in Windows10 Machine and I have configured a simple job to test out few things. Under Build section in Jenkins, I have used "Execute Windows Batch Command" and used the following two commands. Both the below commands executes fine in the command prompt but however the Jenkins Build job keeps getting failed with Exit 1 state.
date
echo "SampleBuild job completed successfully."
Couldn't able to get the reason for the failure. And the following is what we see in console output.
Started by user Administrator
Building in workspace C:\ProgramData\Jenkins\workspace\SampleBuildJob
[SampleBuildJob] $ cmd /c call C:\WINDOWS\TEMP\jenkins6658106255890809140.bat
C:\ProgramData\Jenkins\workspace\SampleBuildJob>date
The current date is: Fri 02/23/2018
Enter the new date: (mm-dd-yy)
C:\ProgramData\Jenkins\workspace\SampleBuildJob>echo "SampleBuild job completed successfully."
"SampleBuild job completed successfully."
C:\ProgramData\Jenkins\workspace\SampleBuildJob>exit 1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
Can anyone tell me, what am I missing?
Try to add (call ) to end of your Batch Command.
This will clear the errorlevel environment variable, there for force the build result to be successful.
If you want to check a specific command's result to determine the build result. Let's say you want to check dates command.
(dates) || (exit %errorlevel% )
This will fail the build if error happens in the first command.
or
(dates) && (echo "command executed succesfully!")
This will show the message only when the first command successfully executed.
Then with the changed command, you do not need (call ) any more.
This windows batch script will work:
echo %DATE%
echo %Time%
echo "command executed succesfully!"
The windows batch script show the same error to me.
Finally i have added exit 0 at the end it worked

getting echo from batch script when background task ends

I have this kind of batch file
start /b ruby script.rb && echo done
...
However, it write 'done' immediately after i run the batch file, which is incorrect, as the script took about 5 mins.
So, how to echo done only after the bg task succesfully end?
thank you!
note: I think that && operator work this way if it is not used in batch file nor in usage to run bg task.
start "" /b cmd /c "ruby script.rb && echo done"
Without the quotes the conditional execution command is seen as the continuation of the start command instead of continuation of the ruby command.
As the && is an operator handled by cmd we need to start a new instance.
The "" at the start are needed as the first quoted argument to start is handled as a window title.
note: && is an conditional execution operator. If the previous command does not generate an error then the following command is executed. In this case with the process running in background, probably, &, the command concatenation operator, should be used to know that the script ended.

cmd terminate application after specified time of execution

I installed linphone application and i am writing a bat file doing a loop through file to make calls through executing this command
echo call %%H%%Z#%%G^|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"
Now the loop is working fine and everything is ok but the problem that linphone is not terminating after making the first command to move on for the remaining of the loop , when i try to use linphone directly thorough the cmd command it remain open until i use quit command like this
C:\Users\administrator>cd C:\Program Files (x86)\Linphone\bin
C:\Program Files (x86)\Linphone\bin>linphonec -s 111#1.1.1.1
WARNING: no real random source present!
Ready
Warning: video is disabled in linphonec, use -V or -C or -D to enable.
linphonec> Establishing call id to sip:111#1.1.1.1, assigned id 1
Contacting sip:111#1.1.1.1
linphonec> Call 1 to sip:111#1.1.1.1 in progress.
linphonec> quit
Terminating...
Call ended
linphonec> Call 1 with sip:111#1.1.1.1 ended (No error).
No response.
linphonec>
C:\Program Files (x86)\Linphone\bin>
how i can terminate the program in my command above after executing the call command ?
I have not linphone to test, but if the program accepts piped commands, maybe, this could work
(echo call %%H%%Z#%%G&echo quit)|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"
edited to make it wait
(
echo call %%H%%Z#%%G
ping -n 11 localhost >nul 2>nul
echo quit
) | "C:\Program Files (x86)\Linphone\bin\linphonec.exe"
or
(echo call %%H%%Z#%%G& ping -n 11 localhost >nul 2>nul & echo quit)|"C:\Program Files (x86)\Linphone\bin\linphonec.exe"

Command Line & and &&

so currently what I am attempting to do is, remote onto a different server, launch a scheduled task, exit, change to a specified folder on my desktop, write a file
My code currently looks as such
C:MyOriginalFolder> psexec \\MYREMOTESERVER -u MYUSERNAME cmd
C:MYREMOTESERVER> SCHTASK.......
C:MYREMOTESERVER> exit & cd C:\\Users\ce132d & echo "Logged off" > MyLog.txt
//expected: the folder C:\\Users\ce132d should have a text file called MyLog.txt
//what happens: I end up in C:MyOriginalFolder with no MyLog.txt file created
When I remove the &'s and test it command by command, all is dandy and the expected behavior happens. But when linking them together with & and &&, the expected behavior does not happen.
So my question is this: is there some way of one-lining the actions of exiting, changing directory, and writing a text file?
I am eventually going to check if loging into the remote server was successful or not, and want to put those 3 actions into a if (successful login) {do 3 tasks} else {write error log}..
You send your remote server exit & cd C:\\Users\ce132d & echo "Logged off" > MyLog.txt.
Why do you expect that the cd ... should have any effect on your local directory?
When you send the commands line by line then your exit will EXIT the remote server context, then you are again on your local machine, therefore the rest of the commands works as expected.
The > MyLog.txt is being evaluated before any of the commands are run, including the cd. You can use > C:\Users\ce132d\MyLog.txt instead.
You can also use >> instead of > to append to a file.

Resources