Find out how many parent processes a batch-file is running under - windows

I've got a batch-file that is calling itself in multiple CMD sessions, for example:
#echo off
if "%var%"=="set" goto :begin
set var=set
call cmd /c %0
:begin
echo Inside CMD session! Executed under x processes
pause
What I'm trying to accomplish is to get the number of processes a batch-file is being called from.
Normal batch file:
| Batch-file
| Batch script
Would return 1, as the batch-file is being called from the root process
Multiple process batch-file:
| Batch-file
| CMD
| Batch-file
| Batch script
Would return 2, as the batch-file is being called from another batch-file.
A possible solution would be to get the root process identifier number (PID), and then analyse the wait chain for that process, which I'm able to accomplish easily in Task Manager:
Summary:
How can I return the number of processes a batch-file is being executed under, with or without any third-party utilities?

#echo off
setlocal
if "%var%"=="3" goto :begin
set /A var+=1
cmd /C "%~F0"
goto :EOF
:begin
echo Inside CMD session!
wmic process where "name='cmd.exe'" get ExecutablePath,ParentProcessId,ProcessId > wmic.txt
for /F "skip=1 tokens=1-3" %%a in ('type wmic.txt') do (
ECHO %%a - Parent: %%b - PID: %%c
set "Parent[%%c]=%%b"
set "This=%%c"
)
set X=0
:nextParent
set /A X+=1
call set "This=%%Parent[%This%]%%"
if defined Parent[%This%] goto nextParent
echo Executed under %X% processes
Output example:
Inside CMD session!
C:\Windows\system32\cmd.exe - Parent: 3792 - PID: 4416
C:\Windows\system32\cmd.exe - Parent: 4416 - PID: 3220
C:\Windows\system32\cmd.exe - Parent: 3220 - PID: 1728
C:\Windows\system32\cmd.exe - Parent: 1728 - PID: 3560
Executed under 4 processes

I dabbled with Aacini's code to have the tree more visual
Inside CMD session
C:\WINDOWS\system32\cmd.exe - 1004:12424
C:\WINDOWS\system32\cmd.exe - 12424:12016
C:\WINDOWS\system32\cmd.exe - 12016:10592
C:\WINDOWS\system32\cmd.exe - 10592:11392
C:\WINDOWS\system32\cmd.exe - 11392:5616
Executed under 5 processes
Changed the middle part
Set "Space="
wmic process get ExecutablePath,ParentProcessId,ProcessId > wmic.txt
for /F "tokens=1-3" %%a in ('type wmic.txt') do (
if /I "%%a" equ "%ComSpec%" (
Call ECHO %%a - %%Space%%%%b:%%c
CAll Set "Space=%%Space%% "
set "Parent[%%c]=%%b"
set "This=%%c"
)
)

#echo off
setlocal
if "%var%"=="3" goto :begin
set /A var+=1
cmd /C "%~F0"
goto :EOF
:begin
echo Inside CMD session!
set /p Space=
wmic process get ExecutablePath,ParentProcessId,ProcessId > wmic.txt
for /F "tokens=1-3" %%a in ('type wmic.txt') do (
if /I "%%a" equ "%ComSpec%" (
Call ECHO %%a - %%Space%%%%b:%%c
CAll Set "Space=%%Space%% "
set "Parent[%%c]=%%b"
set "This=%%c"
)
)
set X=0
:nextParent
set /A X+=1
call set "This=%%Parent[%This%]%%"
if defined Parent[%This%] goto nextParent
echo Executed under %X% processes
pause

Related

Windows batch: combining start and FOR /f "tokens=1" with | escape coded symbols

I am trying to execute this command from standard windows batch file.
start /B for /f "tokens=1" %%a in ('Query SESSION ^| find /i "rdp"') do (echo yes |reset session %%a)
and it throws an error:
"| was unexpected at this time."
I just found that it fails because start in the beginning.
What's wrong with it?
From a batch file it's a bad idea to try to build a valid one line solution with start, it's very tricky to escape/quote a complex expression.
But it's quiet easy to use start and jump to a label in the same batch file.
#echo off
REM *** Thread redirector
for /F "tokens=3 delims=:" %%F in ("%~0") do goto %%F
REM *** Start a new thread in this batch file at the label :myThread
start "" /b cmd /c "%~d0\:myThread:\..\%~pnx0"
echo Main
for /L %%n in (1 1 10) DO (
echo MainThread doing some stuff %%n
ping localhost -n 2 > nul
)
exit /b
:myThread
for /f "tokens=1" %%a in ('Query SESSION ^| find /i "rdp"') do (
echo myThread: Resetting session %%a
echo yes | reset session %%a
)
exit /b

Executing shell commands in parallel but limiting jobs (Windows without Cygwin)

Here is what I am trying to do. Suppose I have a program called myprogram.exe, which I have to execute 1000 times.
Under Windows, I could usually do something as simple as:
for /L %n in (1,1,1000) do start /myfolder/myprogram.exe
However, suppose I only have 5 CPU threads I can devote to running the 1000 instances of myprogram.exe, such that I launch only 5, then when one of these finishes another one is launched, etc until the whole 1000 end.
Under Linux and using GNU Parallel, I could simply do:
seq 1000 | parallel -N0 -j5 "nohup myprogram.exe"
How could I achieve something like that in Windows command line? Notice that in my case using Cygwin is not an option, so resorting to xargs and GNU Parallel under Windows are not options either.
Here is a way, by using powershell to do the process count. and using a simply set /a as counter.
#echo off
setlocal enabledelayedexpansion
set /a cnt=0
:counter
if !cnt! lss 1000 (
for /F "tokens=*" %%i in ('powershell ^(Get-Process -Name 'myprogram'^).count') do set proc=%%i
if !proc! lss 5 (
start "C:\myfolder\myprogram.exe"
set /a cnt+=1
)
goto :counter
)
You could add echo !cnt! in the line before goto :counter if you want to see it count.
It can be done without using delayedexpansion but I prefer to use it here.
This will run five processes in parallel. Each time one of them finishes, the next process will be started (so there are always 5 of them until they all are done)
#ECHO off
setlocal enabledelayedexpansion
set bunch=5
for /l %%a in (1,1,1000) do (
call :loop
echo processing: %%a
start "MyCommand" cmd /c timeout !random:~-1!
)
call :loop
goto :eof
:loop REM waits for available slot
for /f %%x in ('tasklist /fi "windowtitle eq MyCommand" ^| find /c "cmd.exe"') do set x=%%x
if %x% geq %bunch% goto :loop
goto :eof
Add the /min switch to start to minimize the started processes.
Give them a unique windowtitle (MyCommand here) to be able to count them.
Replace cmd /c timeout !random:~-1! with your actual command.
EDIT a slightly modified script, which may work better, if myprogram is a GUI (script above will work better with CLI applications):
#ECHO off
setlocal enabledelayedexpansion
set bunch=5
for /l %%a in (1,1,100) do (
call :loop
echo processing: %%a
start notepad.exe
)
call :loop
goto :eof
:loop REM waits for available slot
for /f %%x in ('tasklist /fi "imagename eq Notepad.exe"^|find /c "."') do set x=%%x
if %x% geq %bunch% goto :loop
goto :eof

Running two command prompt terminals in native windows, how can I redirect the output of one to the input of the other?

I have one Windows 10 command prompt running and awaiting input, and I wish to automate continuous and live input with a second command prompt. I have gotten the second command prompt to extract the desired variable, and I wish to send it to the other command prompt that is waiting for input.
The "awaiting input" command prompt must run in real time because it is connected to Plink (not an SSH session so no use of the -m command here) which is connecting to a microcontroller. So it cannot be accomplished (at least I don't think) with function calls.
I see that it can be done in UNIX environments: https://askubuntu.com/questions/496914/write-command-in-one-terminal-see-result-on-other-one
Thanks in advance and please advise,
--A hopeful beginner
Batch code starts 2 piped processes, one for getting keyboard input and writing to a file, and the other reading the data written. Note there isn't a cmd window for each process, but there are two new processes running. You may use cmd /c or start if you need two consoles.
Does it help?
#echo off
set "pipefile=pipefile.txt"
if "%~1" neq "" goto %1
copy nul "%pipefile%" >nul
"%~F0" getInput >>"%pipefile%" | "%~F0" readInput <"%pipefile%"
echo(
echo(Batch end...
ping localhost -n 1 >nul
del /F /Q "%pipefile%" 2>nul
exit/B
:getInput
set "input="
set/P "input="<CON
echo(%input%
if /I "%input%" equ "EXIT" exit
goto:getInput
:readInput
setlocal enableDelayedExpansion
set/P="enter some data [EXIT to exit] "
for /l %%. in () do (
set "input="
set/P "input="
if defined input (
if /I "!input!" equ "EXIT" exit
set/P="enter some data [EXIT to exit] "
)
ping 1.1.1.1 -w 10 -n 1 >nul 2>nul & rem avoid processor load (may be removed)
)
Running two console windows
#echo off
set "pipefile=pipefile.txt"
if "%~1" neq "" goto %1
del /F /Q "%pipefile%" 2>nul
copy nul "%pipefile%" >nul
start "writer" cmd /c ^""%~f0" readInput ^<"%pipefile%" ^"
start "reader" cmd /c ^""%~f0" getInput ^"
echo(
Echo batch end...
ping localhost -n 1 >nul
goto :EOF
:getInput
set "input="
set/P "input=enter some data [EXIT to exit] "
echo(%input%>>"%pipefile%"
if /I "%input%" equ "EXIT" exit
goto:getInput
:readInput
setlocal enableDelayedExpansion
for /l %%. in () do (
set "input="
set /p "input="
if defined input (
if /I "!input!" equ "EXIT" exit
echo(!input!
)
ping 1.1.1.1 -w 10 -n 1 >nul 2>nul & rem avoid processor load (may be removed)
)

Batch file progress percentage

How can I show the progress of a long running operation in windows batch (cmd) file in percentage? Can you share some example code?
Here is how...
Note: This code is a slightly modified version of this answer.
#echo off
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
FOR /L %%n in (1,1,10) DO (
call :show_progress %%n 10
ping localhost -n 2 > nul
)
echo Done!
exit /b
:show_progress
setlocal EnableDelayedExpansion
set current_step=%1
set total_steps=%2
set /a "progress=(current_step * 100) / total_steps"
set /p ".=Progress: !progress!%%!CR!" <nul
if !progress! equ 100 echo.
exit /b
An example would be scanning a large file/database, showing progress instead of a blinking cursor, for ages!
set c=<no. of lines in file>
for /l %i in (1,1,%c%) do cls & set /p="Scanning for target ... "<nul & (set /a per=%i00/%c% >nul & echo !per!%)& ping 127.0.0.1 -n 2 > nul & REM when target found, send agents to greet
echo Complete.
The code in brackets () are for progress percentage.
Tested in Win 10 CmD>

Limiting the number of spawned processes in batch script

I have a situation very similar to the one described in this question (but in batch, not shell). I made a simple batch script to iterate through the lines of a tile and download data from a server using a python script (the process itself is more complicated than just a simple download, it has to authenticate with an API and fetch several URLs).
The first version was as follows:
for /F "tokens=*" %%A in (client_name_list.txt) do python download_metadata.py "%%A"
The way it is it waits until each iteration is done to move on, so I updated it to the following:
for /F "tokens=*" %%A in (client_name_list.txt) do start cmd /C python download_metadata.py "%%A"
The second versions does what I want to but, as the file client_name_list.txt is about 30,000 lines long, a lot of command prompts start spawning and the computers freezes within seconds.
How do I limit the number of running instances of CMD (to, for example 10) and make the script wait until there is a "free CMD slot" to go the next line?
Adapted from my answer to "Parallel execution of shell processes". Follow the link to get an explanation.
#echo off
setlocal enableDelayedExpansion
:: Display the output of each process if the /O option is used
:: else ignore the output of each process
if /i "%~1" equ "/O" (
set "lockHandle=1"
set "showOutput=1"
) else (
set "lockHandle=1^>nul 9"
set "showOutput="
)
:: Define the maximum number of parallel processes to run.
set "maxProc=10"
:: Get a unique base lock name for this particular instantiation.
:: Incorporate a timestamp from WMIC if possible, but don't fail if
:: WMIC not available. Also incorporate a random number.
set "lock="
for /f "skip=1 delims=-+ " %%T in ('2^>nul wmic os get localdatetime') do (
set "lock=%%T"
goto :break
)
:break
set "lock=%temp%\lock%lock%_%random%_"
:: Initialize the counters
set /a "startCount=0, endCount=0"
:: Clear any existing end flags
for /l %%N in (1 1 %maxProc%) do set "endProc%%N="
:: Launch the commands in a loop
set launch=1
for /f "delims=" %%A in (client_name_list.txt) do (
if !startCount! lss %maxProc% (
set /a "startCount+=1, nextProc=startCount"
) else (
call :wait
)
set cmd!nextProc!=%%A
if defined showOutput echo -------------------------------------------------------------------------------
echo !time! - proc!nextProc!: starting %%A
2>nul del %lock%!nextProc!
%= Redirect the lock handle to the lock file. The CMD process will =%
%= maintain an exclusive lock on the lock file until the process ends. =%
start /b "" cmd /c %lockHandle%^>"%lock%!nextProc!" 2^>^&1 python download_metadata.py "%%A"
)
set "launch="
:wait
:: Wait for procs to finish in a loop
:: If still launching then return as soon as a proc ends
:: else wait for all procs to finish
:: redirect stderr to null to suppress any error message if redirection
:: within the loop fails.
for /l %%N in (1 1 %startCount%) do 2>nul (
%= Redirect an unused file handle to the lock file. If the process is =%
%= still running then redirection will fail and the IF body will not run =%
if not defined endProc%%N if exist "%lock%%%N" 9>>"%lock%%%N" (
%= Made it inside the IF body so the process must have finished =%
if defined showOutput echo ===============================================================================
echo !time! - proc%%N: finished !cmd%%N!
if defined showOutput type "%lock%%%N"
if defined launch (
set nextProc=%%N
exit /b
)
set /a "endCount+=1, endProc%%N=1"
)
)
if %endCount% lss %startCount% (
1>nul 2>nul ping /n 2 ::1
goto :wait
)
2>nul del %lock%*
if defined showOutput echo ===============================================================================
echo Done
In each iteration of your for loop you can count the number of CMD task open. If the value is lesser than the limit you start a new task else you wait until one slot is free.
#echo off
set $Limit=11
setlocal enabledelayedexpansion
for /F "tokens=*" %%A in (client_name_list.txt) do (call:wait %%A)
exit/b
:wait
set "$cmd="
for /f %%a in ('tasklist ^| findstr /i "cmd"') do set /a $cmd+=1
if !$cmd! lss %$Limit% (
start cmd /C python download_metadata.py "%1"
goto:eof)
ping localhost -n 2 >nul
goto:wait

Resources