cygwin getting invoked and end of stack error in bat file - batch-file

I am running the following bat file and got an error
assertion "root_idx != -1" failed:
file "/ext/build/netrel/src/cygwin-1.7.1-1/winsup/cygwin/mount.cc",
line 363, function: void mount_info::init()
Stack trace: Some random stack trace
ENd of stack Trace
batch code
#echo off
:top
set /a run=1
echo "Beginning of Loop"
echo %run%
wmic process where "name like '%%java%%' and CommandLine like '%%abc%%'" get caption, CommandLine | findstr "abc" 1>nul || set /a run=0
if %run%==0 (
echo "Process Not Running"
start cmd /c 'path to file')
echo "Taking a Pause"
sleep 10
goto :top
Why is cygwin getting invoked in the first place?
and why did i get the error and the infinite loop halted (the loop checks for the existence of a program and starts it if it is killed somehow

Don't know how to avoid the Cygwin error but if you don't want it to be invoked, check your PATH variable so that the Cygwin path is not in it, or at least it is at the end of the path list.

This is a Cygwin bug http://cygwin.1069669.n5.nabble.com/cygwin-crash-assertion-quot-root-idx-1-quot-failed-reliably-reproduced-on-my-system-td2659.html.
In my case it started to reproduce after some build processes (and the Cygwin consoles associated with them) got into Suspended state. I saw it with the Process Explorer tool (the one from https://live.sysinternals.com/).
I don't know the cause why they were suspended. Perhaps the parent process was killed at the moment they waited for something. It does not really matter. But what does matter is that after that every new Cygwin console hanged when I opened it. The fix for me was to find the old suspended Cygwin processes and kill them.
Why is cygwin getting invoked in the first place?
When you run something in the cmd.exe, but your PATH is configured to use Cygwin it tries to run your command in Cygwin first (depending on the order in the PATH).
why did i get the error and the infinite loop halted
I suppose you had the same issue like me. That is every new Cygwin console hangs until you kill the first problematic one.

Related

How to stop a .bat process from within

So, I have a program for the Windows Command Prompt, which is for changing the directory (so I can go to a language directory without having to do cd everytime)
and I want to kill it after I select an option. However, I have tried some
methods, which do one of the following:
a. taskkill /IM ... (blows up with a process not found error)
and
b. exit [as shown here] (does what I want, but it also closes the command prompt)
This is my program (the important part):
:C++
cd C:\Users\S.G.\Documents\C++ Scripts
echo What's in C++ Scripts:
dir
pause
exit
:Python
cd C:\Users\S.G.\AppData\Local\Programs\Python\Python36-32
echo What's in Python:
dir
exit
The reason why I'm stuck as to how one does this is because say I choose option "C++".
If I choose it, it runs what I have described, but it also runs the "Python" function. If I run the "Python"function however, it runs fine and doesn't display whatever's in the "C++" function.
Why is function "C++" also running "Python" when I intend not to?
You should use exit /b, followed by an optional error code (eg. exit /b 0). An alternative way to do this is to skip to the end of the file using GOTO:EOF.

Using call <file.bat> results in "sleep is not recognized as an internal or external command.."

I have a script that calls other commands in a for loop:
for %%x in (%CMDS::= %) do (
call C:\%%x %1%
echo "%%x complete"
)
However, running this results the console spitting out :
'sleep' is not recognized as an internal or external command,
operable program or batch file.
This is because the files i loop through and run have these commands in them. Why is it that if i run these files one by one they work, but when chained using call they don't? I can sleep in my terminal outside of this script..
Regards
Thanks to another answer, I solved this error by replacing sleep 5 in my .bat file with:
powershell -Command "& {sleep 5}"
Works fine now. Better still, also tested Stephan's suggestion:
timeout 5
Simpler, and shows a nice message like
Waiting for 0 seconds, press a key to continue ...
Note that some Windows versions require the /t option to define the time.
timeout /t 5
There is no sleep command in batch. That's why you are getting this error.
EDIT:
There is no sleep command in Windows CMD or Batch. BUT: as you can use the command in your console, I suppose there might be a script or a program called sleep. This script or program might be situated in your working directory or in some other directory included in your %PATH% variable. If this is the case, it's possible that your script gives you this error because of a path issue.
Say, you are in C:\SomeFolder and there is a sleep.exe in there. You are calling another script or command which changes the current directory to D:\AnotherFolder. Now another script or command tries to execute your mysterious sleep command assuming the working dir to be C:\SomeFolder but as you are in a different folder (D:\SnotherFolder) now, sleep can't be found. Further, when using call the variable scope of the calling script becomes also the scope for the called script. So it's also possible that variables are being overwritten by different scripts. Such a variable might contain the path to your sleep command. This could also cause an error.

Windows Batch: Executing command with FOR /F -- "command not found"

I have a problem with executing an other command within a FOR-command on Windows 7 Ultimate.
The for-command is part of a batch-file and should parse the outputs of another command.
Usually this:
for /f %%a IN ('tasklist') DO echo %%a
should execute the command "Tasklist" and output its results via echo.
But I always get "command not found".
I tried to execute the command outside of the forloop and it works.
I also tried do execute lots of outer commands within the for, but every command said "command not found".
I also tried the examples of this post:
Batch: Execute command with quotes in for loop with piping to find
I`m sure
that the commands exists
that I have read and execute rights to it
that my User is in Administrator Group
that I run the commands with "Run as Administrator"
But nothing is working in this pc.
The same commands work on another PC wich is also running a windows 7 ultimate.
So has anybody an idea would could be wrong on the pc where all commands are not found ?
Here is an example of my console outputs when I try it with the command "ls".
ls.exe is a executable file from the gun4win project, and its located in the same folder where my batch-file is running.
The windows is in german, so the error output is also in german.
C:\test>test_for.bat
C:\test>rem --- test a command stand-alone ---
C:\test>ls
ls.exe test_for.bat
C:\test>rem --- test same command in a FOR-Loop ---
C:\test>for /F "delims=" %a in ('ls') do echo FOR-OUTPUT: %a
Der Befehl "ls" ist entweder falsch geschrieben oder konnte nicht gefunden
werden.
FINAL EDIT:
The problem was as wrong value for the system envoirement variable ComSpec.
I changed ComSpec in Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen" to "C:\Windows\system32\cmd.exe and the problem was solved.
Thanks to #foxidrive and #jeb
If you use FOR /F and get the error 'something' is not recognized as an internal or external command, operable program or batch file for every program, even internal commands, the most common cause is a wrong ComSpec variable.
You can check the variable with set ComSpec, it should be C:\Windows\System32\cmd.exe.
It will not help to change the variable on the command line, the cause is described at DosTips: ComSpec strange behaviour
If the variable contains a different value, you should correct this under
For a German system:
Erweiterte Systemeigenschaften->Erweitert->Umgebungsvariaben->Systemvariablen
For an English system:
Win Key+Pause Key->Advanced System Settings->Environment Variables->System Variables
There exists a second possible cause for strange FOR /F behaviour
If the AutoRun feature can be enabled in the registry ...\Command Processor\AutoRun, for more details see cmd /?.
The AutoRun feature can start a batch file each time a new cmd.exe instance is started.
This can be useful for ex. showing some data on opening a new cmd window or always change to a choosen directory.
But this batch will be also executed inside the FOR /F and normally causes unexpected results.
Pipes also start new cmd instances, but suppress the AutoRun script

Do batch files support exit traps?

Do Windows .bat scripts have an exit-trap like feature, that would allow registration of cleanup actions that always need to be run?
I am not interested in any solution that uses Powershell or other shells (I already have Python installed on the target system so if that can't be done in batch files I'll just wrap a bunch of calls to subprocess.Popen in try...finally).
No, there are no exceptions, and no formal way of registering an exit routine to run automatically upon termination.
However, you can get close if you do not need environment changes to persist after the script terminates. You can run the majority of your script in a new CMD session, and then you can have cleanup routines in the parent level that will always run upon termination as long as the console window is not killed. Control will be returned back to the outer shell if the inner script terminates normally, or terminates due to Ctrl-C, or terminates due to fatal syntax error, etc.
Here is a trivial demonstration:
#echo off
if "%~1" equ ":main" (
shift /1
goto main
)
cmd /d /c "%~f0" :main %*
echo Cleanup actions go here
exit /b
:main
echo Main actions go here
exit /b
UPDATE - Hacked exceptions
I have since developed an effective exception hack using nothing but native batch commands - See Does Windows batch support exception handling?.
Exceptions can be thrown at any level of your code, and the exception bubbles up until a catch block handles the exception. The code cannot recognize errors on its own - each exception must be explicitly thrown by your code. It also cannot respond to , fatal syntax errors, or closed console. But if those limitations are acceptable, then it is an extremely effective way of installing cleanup code.
Batch doesn't support anything like this.
However, cmd also doesn't have exceptions and very few ways of abnormally terminating a script (a syntax error in your script, or crashing cmd.exe for example). If your script doesn't intentionally terminate itself, it should be safe to simply put any cleanup at the end of your script.
To go further, you can call your batch file in a subshell from another batch file, and have the outer script do your cleanup. Then, no matter what happens in the script, the outer shell will just run the next command in the outer batch when it finishes.

Batch file running under Task Scheduler continues running after batch file completes

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!

Resources