Skip to main content

How to Kill a Slow Process using DOS Batch File Script with TaskKill and TaskList



If you are still trapped in the era of batch files and command lines in Windows environment, here is something you can add to your DOS cheat codes.  Lucky for us here, we are still using DOS batch file to implement installation of some updates to our system. Here is a simple batch file script that is designed for slow systems that requires a significant amount of time to kill a particular process. The requirement is simple,  do not start any copying or moving of files until  the process ( i.e. MyProcess.exe) is totally killed by DOS. The simplest solution would be to use TASKKILL to kill the process, wait for a few seconds  then start copying/ moving of files there after. Unfortunately, while we were testing this simple step on a slow machine, DOS would take an awful lot of minutes to kill the process in question. Thus, when our delay is reached, we would encounter error on copying/moving files that is currently used by the unkilled process causing a failure on the installation. So instead of increasing our delay to God-Knows-How-Much , we went for another approach. That is to continuously try to kill the process ( after a certain  delay ) until it is actually out of the task list. This would mean  a:) continuously monitoring the task list to see if the process is still alive b:) create a loop that will do the monitoring of the task list, try to kill the process, delay for some time and repeat again. 

Here is a simple script that my friend came out to solve the issue. She used TASKKILL to kill the process, TASKLIST to list the currently running process, a temporary file to dump the task list result, PING command to add delay and some FOR and IF statements. This worked on XP machines and also tested on Win2003 machines.


:Loop
    TASKKILL /F /T /IM MyProcess.exe
    echo delaying for 35sec...
    ping 1.1.1.1 -n 1 -w 35000
    TASKLIST /FI "IMAGENAME eq MyProcess.exe" /NH > tmp.txt
      FOR /F %%A IN (tmp.txt) DO (
      IF /I "%%~A"=="MyProcess.exe" (
          GOTO Loop
         )
      )
    del /F /Q tmp.txt

REM Continue the installation process here, after MyProcess.exe has been killed.

Comments

Instead of 'ping 1.1.1.1 -n 1 -w 35000' one could use the timeout utility (e.g. timeout /T 35).

The creation of temp file is not really needed. If Command extensions are enabled (SETLOCAL ENABLEEXTENSIONS) FOR can execute a utility and then parse its output
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

See "FOR /?" for more details...

Popular posts from this blog

Getting Started with Stateless : A Lightweight Workflow Library Alternative for .NET

Image Credit: https://www.pioneerrx.com A year ago, I was looking for a simple workflow manager for a project I was working. Its a medium sized application that involves tracking the state of assets in the system. Back in 2008, Microsoft (MS) introduced new technologies along with the release of Visual Studio 2008: Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), and  Windows Workflow Foundation (WF). Having worked in a company utilizing mostly MS products for development, my first option was to go with WF. After doing some time reading and studying the library, I paused and decided it was too complex for my requirement. Using WF would be an overkill and the fact that it has, a rather, steep learning curve, there has to be another option. My mind toyed with the idea of developing a simple workflow library myself. It would be a learning experience but it might end up consuming a lot of time. Why reinvent the wheel? So I started querying the inte

Hiding Unwanted Python Folders and Files in Visual Studio Code

Visual Studio Code is a universal editor and pretty good at it. However, the explorer view maybe cluttered with the automatically generated folders and files confusing developers. Python is no different. Below are example files and folders generated by Python. The __pycache__ folder and *.pyc files  are totally unnecessary to the developer. To hide these files from the explorer view, we need to edit the settings.json for VSCode. Add the folder and the files as shown below: Copy and paste the lines below : "**/*.pyc" : { "when" : "$(basename).py" }, "**/__pycache__" : true

My First Blog for 2009

Im starting 2009 with a blog on foods. Since my 4 months voluntary exile in the US (hahaha), I started cooking ( full time! ) so Google is my cookbook and I am the chef ( sort of ). Yesterday , I was looking forward for another experiment on the kitchen lab (it gets messy sometimes) . What I have ? Pork belly , Chinese Okra , Squash . I was having doubts on whether the Chinese Okra is the same thing as the vegetable I know from the province , "kabatiti" . So after searching for "kabatiti" on the net , Google returned some informative links on some Ilocano Foods which made my day . Listed below are some Ilocano food worth mentioning . Abrao or Inabrao - assorted seasonal vegetables, typically malunggay, and that quintessentially Ilocano vegetable, saluyot, boiled in a bagoong and fish broth Ipon—tiny fish in season during the cold months Poki-Poki (also poqui-poqui), an innocent omelet made of eggplant sautéed with garlic, onions, tomatoes, and eggs Kabat