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.

ECHO OFF
SET ProcessName=notepad.exe
SET StartCommand=notepad.exe
SET WorkingDrive=c:
SET WorkingDirectory=c:\WorkingDirectory
ECHO "Watching for process %ProcessName%"
:Again
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
    %WorkingDrive%
    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.

Mastodon: @j2inet@masto.ai
Instagram: @j2inet
Facebook: @j2inet
YouTube: @j2inet
Telegram: j2inet
Twitter: @j2inet

“listen EACCES: permission denied 127.0.0.1:443”

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 127.0.0.1:443

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    0.0.0.0:80             0.0.0.0:0              LISTENING       4
   TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       1348
   TCP    0.0.0.0:443            0.0.0.0:0              LISTENING       38880
   TCP    0.0.0.0:445            0.0.0.0:0              LISTENING       4
   TCP    0.0.0.0:902            0.0.0.0:0              LISTENING       39848
   TCP    0.0.0.0:912            0.0.0.0:0              LISTENING       39848
   TCP    0.0.0.0:2179           0.0.0.0:0              LISTENING       2304
   TCP    0.0.0.0:2869           0.0.0.0:0              LISTENING       4
   TCP    0.0.0.0:3389           0.0.0.0:0              LISTENING       1600
   TCP    0.0.0.0:5040           0.0.0.0:0              LISTENING       884
   TCP    0.0.0.0:5800           0.0.0.0:0              LISTENING       5252
   TCP    0.0.0.0:5900           0.0.0.0:0              LISTENING       5252
   TCP    0.0.0.0:7680           0.0.0.0:0              LISTENING       37976
   TCP    0.0.0.0:27036          0.0.0.0:0              LISTENING       17928
   TCP    0.0.0.0:44367          0.0.0.0:0              LISTENING       4
   TCP    0.0.0.0:49664          0.0.0.0:0              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    0.0.0.0:443            0.0.0.0:0              LISTENING       38880
   TCP    0.0.0.0:44367          0.0.0.0:0              LISTENING       4
   TCP    192.168.1.81:49166     72.21.81.200:443       TIME_WAIT       0
   TCP    192.168.1.81:49169     64.233.177.101:443     TIME_WAIT       0
   TCP    192.168.1.81:49206     13.249.111.97:443      ESTABLISHED     24324
   TCP    192.168.1.81:49209     52.167.253.237:443     ESTABLISHED     1996
   TCP    192.168.1.81:49220     52.184.216.246:443     ESTABLISHED     37976
   TCP    192.168.1.81:49222     168.62.57.154:443      ESTABLISHED     24324
   TCP    192.168.1.81:49224     52.114.74.45:443       ESTABLISHED     13304
   TCP    192.168.1.81:49227     52.113.194.132:443     ESTABLISHED     10376
   TCP    192.168.1.81:49228     184.24.37.85:443       ESTABLISHED     27828
   TCP    192.168.1.81:49231     13.92.225.245:443      ESTABLISHED     27828
   TCP    192.168.1.81:49233     140.82.113.3:443       ESTABLISHED     24324
   TCP    192.168.1.81:49234     20.190.133.75:443      ESTABLISHED     39168
   TCP    192.168.1.81:49236     204.79.197.203:443     ESTABLISHED     27828
   TCP    192.168.1.81:49238     52.96.104.18:443       ESTABLISHED     12440
   TCP    192.168.1.81:49239     52.96.104.18:443       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