Start /b command is not completing the command and exit SSH straightaway - windows

I am using ssh to run a bat file which will be running multiple commands in parallel as below:
(
start "" cmd /c "cd "C:/Users/desktop" && python -m command"
start "" cmd /c "cd "C:/Users/desktop" && python -m command"
) | pause >nul
the bat file is able to run however, i requrie the output of the python to be shown in the command window. I have tried to include /b in the command
(
start /b "" cmd /c "cd "C:/Users/desktop" && python -m command"
start /b "" cmd /c "cd "C:/Users/desktop" && python -m command"
) | pause >nul
However, the above does not work. It exited before finishing the run of the python commands.

Related

How to use && inside echo without using quotes?

I want to write a command like this inside a file using only cmd.exe (with ShellExecute, C++),
It's something like this:
timeout /t 10 && start cmd.exe /C "ssh -o ..."
I tried using this:
echo timeout /t 10 && start cmd.exe /C "ssh -o ..." > myfile.bat
but it cuts when it reaches && and I can't use quotes on the entire string, so what should I do? I don't want to use anything other than cmd.exe, and I just want to write this to a .bat file.
How can I solve this?
1) Use disappearing quotes.
FOR %%^" in ("") do (
echo %%~"timeout /t 10 && start cmd.exe /C "ssh -o ..." %%~" > myfile.bat
)
2) Escape the special characters
echo timeout /t 10 ^&^& start cmd.exe /C "ssh -o ..." > myfile.bat
3) Use delayed expansion
setlocal EnableDelayedExpansion
set "line=timeout /t 10 && start cmd.exe /C "ssh -o ...""
echo !line! > myFile

Run multiple cmd window and execute command parallely

What I need is a batch file when opened, it should execute webpack -w command in 3 different folders in 3 different cmd window
What I tried
start
cd P:\localFolder\FolderA
webpack -w
start
cd P:\localFolder\FolderB
webpack -w
start
cd P:\localFolder\FolderC
webpack -w
This will navigate to first folder and executes the webpack -w command and opens one more command window(without changing to specified directory)
Finally the shorter first
start /d P:\localFolder\FolderA webpack -w
start /d P:\localFolder\FolderB webpack -w
start /d P:\localFolder\FolderC webpack -w
to run asynchronously the command should be passed to start
start cmd /c "cd P:\localFolder\FolderA & webpack -w & pause"
start cmd /c "cd P:\localFolder\FolderB & webpack -w & pause"
start cmd /c "cd P:\localFolder\FolderC & webpack -w & pause"
& is the command delimiter
EDIT after comment and, the first & can be changed to &&, to avoid doing the second command if the first fails
start cmd /c "cd /d P:\localFolder\FolderA && webpack -w & pause"
start cmd /c "cd /d P:\localFolder\FolderB && webpack -w & pause"
start cmd /c "cd /d P:\localFolder\FolderC && webpack -w & pause"
the last & pause is just to see the output after execution
for documentation use autodocumented commands in a cmd
start /?

How to run commands on another cmd

I want to run commands on 2 cmds while opening the same file, like if I open a .bat file It opens 2 cmd and they run 2 differents commands (1 each). It's possible to do that?
If I got you right this is what you want to do, note it's a batch file:
#echo off
start cmd /c "echo 1st command && pause"
start cmd /c "echo 2nd command && pause"
Read about cmd here and about start here. The following switches of the cmd command can be considered:
/c: Carries out the command specified by string and then stops.
/k: Carries out the command specified by string and continues.
Instead of using /k I used /c with a pause command to show the concatenation of 2 commands here.
To concate 2 commands use commandA && commandB which is described here at ss64 which is a great site when it comes to batch scripting:
commandA && commandB: Run commandA, if it succeeds then run commandB
As requested another example with cd, dir and pause could look like:
#echo off
start cmd /c "cd C:\Users\ && dir && pause"
start cmd /c "cd C:\ && dir && pause"
It changes the directory, prints the directory list and wait for use input.

Replacing multiline batch file with a for loop which reads a text file

I currently have a batch file that schedules chkdsk to run on multiple servers pending the next reboot-
c:\pstools\psexec \\server1 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"
c:\pstools\psexec \\server2 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"
c:\pstools\psexec \\server3 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"
c:\pstools\psexec \\server4 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"
c:\pstools\psexec \\server5 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"
I am trying to understand how to construct a FOR loop which will read from a txt file composed of -
server1
server2
server3
server4
The txt file will be located in the data folder on my local e: drive. Also, is there any way to not have to enter the password for the user ID more than once? Appreciate the guidance! Thx and a Happy New Year to all!
You could use a FOR, but you could also create a file named servers.txt that contains:
server1
server2
server3
server4
and run this command:
c:\pstools\psexec #servers.txt ...your command...
PsExec will execute the command on each of the computers listed in the file. Please see the psexec documentation here, and see the #file parameter.
Here's a for loop method:
#echo off
for /f "delims=" %%a in (servers.txt) do (
c:\pstools\psexec \\%%a -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"
)

How do I run another command in a cmd batch file?

I have this command that i need to run
cd /d C:\leads\ssh & C:\Windows\System32\cmd.exe /E:ON /K C:\Ruby192\bin\setrbvars.bat
this opens the command prompt
but I need to run this command in the prompt
ruby C:\lead\leads.rb
which will fire off a script.....but i have no idea what to add to my bat file to do this
i tried the -f flag to tell it to run the command but no go....any ideas to what to do to make this run
cd /d C:\leads\ssh & C:\Windows\System32\cmd.exe /E:ON /K C:\Ruby192\bin\setrbvars.bat -f ruby C:\lead\leads.rb
pause
Try the following batch file:
#echo off
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\lead\leads.rb

Resources