CMD.exe consumes all the CPU, blocking other CMD.exe to execute - windows

We have some Batch scripts (.bat) in Windows to execute the “backups” and “archive log” for the databases. These scripts are called from Tivoli periodically.
For each executed script, the process creates a sub session in order to load the DB2cmd environment, in order to execute the db2 commands and exit.
daily.bat
call db2cmd hourly.cmd
The content of the script is this:
db2_job_saveddaily.cmd
db2 -fE:\DB2\scripts\tmp\db2_job_savedbhourly.db2 -zE:\DB2\scripts\tmp\db2_job_savedbhourly.log
exit
The content of the db2 file is (however, it is not important because it is executed correctly)
db2_job_saveddaily.db2
archive log for database ICMNLSDB
We are facing a problem with these scripts, and I think it is related with the exit. At one execution, the script freezes, and it starts to consume the whole CPU (see attached image). After this behavior, we cannot execute any other DB2 command from the CLP.
We kill all the CMD.exe and db2bp.exe processes, but the error persists.
There is nothing in the db2diag.log file, and the only solution is to restart the machine.
Probably, the CMD.exe process losses the communication with the db2bp.exe, and the exit cannot be executed. I would like to understand the origin of this problem and learn how to execute db2 processes in Windows.

Our friend #AngocA seems to check into SO often but hasn't been checking this dangling question even though he did something to close it. Let's at least put his answer in here so folk know it's CLOSED by user. :) Courtesy of tonight's Point Pimp. :-D
"The problem was in another db2cmd session where there was an
infinitive loop. This created a scenario when new db2cmd session
blocked because the first session used the whole CPU. – AngocA"

Related

Get linenumber of hanging script

I recently "inherited" a complex PowerShell script that performs tasks in the background via task scheduler. Now we're seeing that the script hangs in some occasions, but until now I'm unable to identify the root cause.
Is there a way to pol or attach a debugger to an already running script so i can get the current line-number without rewriting large portions? In it's current state, the maintainability of the script is sub-par with 20k lines of code.
I tried checking WMI for properties, but found nothing useful. I did found a chronograph script that may be useful https://powershellexplained.com/2017-02-05-Powershell-Chronometer-line-by-line-script-execution-times/ .
I also wrote a debugging wrapper, but the hang only happens in some occasions. I am unable to reproduce on demand.
Thanks
Have you looked at Enter-PSHostProcess ?
The Enter-PSHostProcess cmdlet connects to and enters into an interactive session with a local process. Instead of creating a new process to host PowerShell and run a remote session, the remote, interactive session is run in an existing process that is already running PowerShell. When you are interacting with a remote session on a specified process, you can enumerate running runspaces, and then select a runspace to debug by running either Debug-Runspace or Enable-RunspaceDebug
Enter-PSHostProcess

windows scheduling of a power-shell script is unreliable

I'm attempting to run an automated powershell script all day, everyday. But I'm have problems getting it to run consistently and reliably, the script itself runs fine, it's getting the windows scheduler to run it consistently that's the problem.
The script is invoked every morning by windows scheduler at 1am as powershell.exe with the command arguments:
-windowstyle Normal -NoExit -file "d:\work\PwrShellScripts\FlmToDb_010.ps1"
Once invoked, the script will run continuously until 11pm at night when it will exit.
The script itself works reliably, but the scheduling only works nine times out of ten, once in a while it fails with the error:
Task Scheduler did not launch task "\DailyFlm" because instance "{aa18e048-d8b2-4e16-8737-fc7babbb609e}" of the same task is already running.
The question is, how to get the script to run reliably every day?
Other info that may be relevant…....
The arguments -windowstyle Normal -NoExit mean that the powershell script runs in a command window (rather than as background process) and the window will remain open if the session ends.
This is done for two reasons, firstly it provides a visual indication that the process is actually running, and secondly if the process fails, it allows the error message to be inspected. The powershell script doesn’t include any file logging, so running it in a command prompt also allows me to confirm that the previous days session made a clean exit when it stopped.
One of the issues is that because the process works 90% of the time, if I make any tweaks I have to wait 10 days or more to confirm if they’ve really worked!
I suspect that the issue may be related to the fact that the console remains open (-NoExit) when the script exits. Most of the time windows seems to recognise that although the console is still open, the associated script has exited.
My guess is that occasionally it decides that since the console is open the process is still running. I'm unable to spot any difference between those occasions when the scheduling works fine and those when it doesn't.
Any suggestions?
Updates...
The scheduler fails to start the job on average once every 10 days. I would prefer to keep the script running in the foreground, it makes monitoring it's progress so much easier, and makes it so obvious if it does fail.
the same task is already running.
The script may do what it's supposed while it runs, but there is one flaw... it's not closing properly. It's exactly the -NoExit issue you talk about. When you run a powershell task, the process name is powershell.exe, with it's associated process id, and that's how the task scheduler knows if it's finished or not.
To fix this, I suggest writing a script to kill all powershell.exe processes and scheduling it to run right at 12:55am every day.

Running multiple instances of a Batch file in windows simultaneously?

I have a windows batch file that is invoked by windows scheduler. When I try to have multiple windows scheduler tasks trying to run the batch file simultaneously, the batch file is locked by the first process and the all the other instances fail.
Is there is way in Windows to run multiple instances of batch file simultaneously?
My script is a simple one all it does is:
set java_classpath
java javaClass
There is nothing inherent to batch file mechanics that limits the number of processes that can simultaneously run the same script. The actual batch script is not locked when it is run. In fact, it is possible to modify a batch script while it is running, though that is usually a very bad idea.
But a batch script could take any number of actions that would prevent simultaneous runs. The most obvious is if the script attempts to redirect output to a specific file (constant path and name). The output redirection establishes an exclusive lock that will prevent any other process from obtaining the same lock.
Another possibility is your script could be calling an external command or program that establishes an exclusive lock in some way.
Either way, there should be nothing to prevent multiple processes from launching the same script simultaneously. But if the script establishes an exclusive lock, then one (or more) of the instances may either crash or exit prematurely, or seem to hang, depending on how the failed lock aquisition is handled.
There really isn't any way to be more specific unless you post your actual script. But if it is a long script, then you should attempt to isolate where the problem is occurring before posting.
Windows 8 task scheduler has the following option (on the last, "Settings" tab):
If the task is already running, then the following rule applies:
Do not start a new instance (default)
Run a new instance in parallel
...
Probably you should change this setting. And also, I would suggest you look into http://serverfault.com and post there.
Did you try calling your batchfile by using %systemroot%\cmd.exe /K C:\path\batchfile.bat? With /K each time a new instance of cmd is opened, guess it is the shell not the file making you weird.
to people coming here from google simply looking for a way to run multiple instances of a .bat file simultaneously, a simple way would be this script:
set N=3
for /L %%i in (1,1,%N%) do (
start yourscript.bat
)

How to restart program automatically if it crashes in Windows?

How can I start my program automatically if it crashes on windows 2003 server? Sometimes my program just crashes, is there a way in windows or settings that I can set?
There are several ways to create a process supervisor/guardian process on Windows.
First, is to leverage windows command line capabilities. Create a bat file:
#echo off
:start
start /w "your app to watch.exe"
goto start
start /w will wait for the process to exit. When the process crashes and exits, the bat script will relaunch it.
Another option is to use free supervisor tool https://github.com/chebum/Supervisor. It allows to restart the crashed app, plus it allows to monitor two or more apps at once and it will automatically close these apps when supervisor's window is closed.
The usual approach is to run what is known as a guardian process. This is a separate process, often a service, that monitors the state of the main process. When the guardian detects that the main service has died, it re-spawns it.
To the very best of my knowledge, there is not built in Windows functionality to do this for you.
Notice: running self-looping bat files can be useful, but unless you know what you're doing, they can wreak all kinds of havoc. This goes especially if you run them on startup. You have been warned.
Anyway. I just remembered something from my 286 days, when I played around a lot with BAT files. If you write the file
yourprogram.exe
some other event
the BAT file will run yourprogram, and then pause and wait around in the background until the program exits. After that it will run "some other event". This used to be kind of annoying if you wanted to run multiple things at once, but here it's actually useful. Using this, it's possible to make it run a loop that restarts the program (and reruns the bat file) as soon as it exits. Combine this with https://superuser.com/questions/62525/run-a-completly-hidden-batch-file, and you'll never even see it happening.
The final BAT file ("restart.bat" in this example) will look something like:
c:\[location]\yourprogram.exe
wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"
That's about it. Start the program (on startup via task or even just startup folder) with line 2, and this ought to solve your problem :)
Oh, if you want to stop the loop, just rename the bat file or put "// " in front of the two lines, save it, and exit the program.
If the program you are running requires admin rights, the solution I found was using psexec (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) to run both the program and the bat with elevated privileges. In that case the BAT will look like:
c:\[location]\psexec -h c:\[location]\yourprogram.exe
c:\[location]\psexec -h wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"
Then you run the bat as administrator, or run the second line (without the psexec part) from task scheduler with elevated privileges. BEWARE: running it as a normal user and clicking "no" on the UAC prompt gave me a BSOD, probably because it looped "can't run program because of lacking privileges" a couple of billion times or something :)
You can use RegisterApplicationRestart.
"If you register for restart and the application encounters an
unhandled exception or is not responsive, the user is offered the
opportunity to restart the application; the application is not
automatically restarted without the user's consent. "
For automatic restart without user intervention, there is also RestartOnCrash. Works with all Windows versions.
I was looking for something similar. There are two options to handle this - either you can write a small script by yourself or use something that is already existing.
After some googling I came across this nice list. The blogger has compiled about 8 tools to automatically restart a crashed or closed application.
Unfortunately there are no settings in Windows to automatically restart a regular program when it crashes.
Do you need to actively interact with your application's GUI? Some of the Service Wrappers (designed to run any application as a Windows Service) will monitor your application and restart it when it fails, but be sure investigate Session 0 Isolation to ensure that it won't get in the way.
You may use some special app like BDV SystemEvents or any other. It allows you to specify application which will be started if some another application is closed. Specify the same application as a Condition and as an Action and you will get expected results.

Scripting for safe file backup under windows

I need to back up some large files that are being written to disk by a process. The process is perpetually running, and occasionally dumps large files that need to be moved over the network. Having the process do this itself is not an option, as the process locks out users whilst it is doing file dumps.
So, this runs under a windows machine, and as a primarily linux user, I am not entirely certain how to do this...
Under linux I would simply use a cron job in the folder (I know the glob that will match the output files), then check lsof, to ensure that the file is not being written to, such that I don't try to copy a partially complete file. Data integrity is critical, so I would normally md5 the files before and after the copy.
So I guess my question is -- how does one do this sort of stuff under windows? I feel like I am kneecapped from the start -- I can use python, but I can't emulate lsof, nor cron to do the task scheduling.
I tried looking at "handle" -- but it needs admin privelidges at execution time, which is also not an option. I can't run the backup process as an admin, it has to run with user privs.
Thanks..
Edit: I just realised I could keep the python instance running, with a sleep, so task scheduling is not a problem :)
For replacing cron you can use the "Task Scheduler" in windows to start your script every few minutes (or specific times).
For lsof the question was discussed here : How can I determine whether a specific file is open in Windows?

Resources