Using a Batch File as a Process Watcher

Using a utility that monitors a process and restarts it if it is terminated for any reason is a common need on programs that are driving publicly viewable displays. Were a crash to happen, or if the process were intentionally terminated, we may still need the process to restart. I’ve got several solutions for this, each with their own strengths. I recently needed a solution for this and didn’t have access to my normal solutions. I was able to make what I needed using Windows in-built command line utilities and a batch file. While I have a preference to PowerShell over batch file scripts, I used batch files this time because of constraints from organizational policies. I’m placing a copy of the script here since I thought it might be useful to others.

This script checks to see if the process of interest is running every 10 seconds. If the process is not running, it will start the program in another 5 seconds. Such large delays are not strictly necessary. But I thought it safer to have them should someone make a mistake while modifying this script. In an earlier version of the script I made a type and found myself in a situation where the computer was stuck in a cycle of spawning new processes. Stopping it was difficult because each new spawned process would take focus from the mouse and the keyboard. With the delays, were that to happen again there is sufficient delay to navigate to the command window instance running the batch file and terminate it.

SET ProcessName=notepad.exe
SET StartCommand=notepad.exe
SET WorkingDrive=c:
SET WorkingDirectory=c:\WorkingDirectory
ECHO "Watching for process %ProcessName%"
timeout /t 10 /nobreak > NUL
echo .
tasklist /fi "ImageName eq %ProcessName%" /fo csv 2> NUL | find /I "%ProcessName%" > NUL
if "%ERRORLEVEL%"=="0" (
    echo Program is running
) else (
    echo Program is not running
    timeout /t 5 /nobreak > NUL
    cd %WorkingDirectory%
    start %StartCommand%
goto Again

Posts may contain products with affiliate links. When you make purchases using these links, we receive a small commission at no extra cost to you. Thank you for your support.

Instagram: @j2inet
Facebook: @j2inet
YouTube: @j2inet
Telegram: j2inet
Twitter: @j2inet

“listen EACCES: permission denied”

After some holiday time off I returned to a work project that uses Angular, started it up, and got this error.

An unhandled exception occurred: listen EACCES: permission denied

I’ve seen this error before, but did not immediately realized what caused it. It took me a few minutes to recall the source of the problem. This error occurs when there is another application that is already using the port that Angular is trying to open. In my case, it was a VMWare service that was occupying the port. I stopped the service and my project started up. If it happened to you, how would you know what process is using the port?

On Windows, you can list which processes are using which port with the following command

netstat -aon

You’ll get a full list of ports, addresses, and process IDs.

Active Connections
 Proto  Local Address          Foreign Address        State           PID
   TCP                 LISTENING       4
   TCP                LISTENING       1348
   TCP                LISTENING       38880
   TCP                LISTENING       4
   TCP                LISTENING       39848
   TCP                LISTENING       39848
   TCP               LISTENING       2304
   TCP               LISTENING       4
   TCP               LISTENING       1600
   TCP               LISTENING       884
   TCP               LISTENING       5252
   TCP               LISTENING       5252
   TCP               LISTENING       37976
   TCP              LISTENING       17928
   TCP              LISTENING       4
   TCP              LISTENING       704

If you wanted to filter those results, you can pass the output through “findstr” using a port number as the string to filter by.

C:\Users\Joel>netstat -aon | findstr 443
   TCP                LISTENING       38880
   TCP              LISTENING       4
   TCP       TIME_WAIT       0
   TCP     TIME_WAIT       0
   TCP      ESTABLISHED     24324
   TCP     ESTABLISHED     1996
   TCP     ESTABLISHED     37976
   TCP      ESTABLISHED     24324
   TCP       ESTABLISHED     13304
   TCP     ESTABLISHED     10376
   TCP       ESTABLISHED     27828
   TCP      ESTABLISHED     27828
   TCP       ESTABLISHED     24324
   TCP      ESTABLISHED     39168
   TCP     ESTABLISHED     27828
   TCP       ESTABLISHED     12440
   TCP       ESTABLISHED     12440

You will be more interested in matches from the left column, since that is the port number being used on your machine. Right now, I can see that on my machine the process occupying port 443 is process 38,880. Great, I have a process number. But what can I do with it. There is another command named “tasklist” that list processes names and their process ID. Combined with findstr, I can get the name of the process using the specific port.

C:\Users\Joel>tasklist | findstr 38880
 vmware-hostd.exe             38880 Services                   0     32,084 K