Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey all!

I'm trying to create a windows service that monitors a port (localhost:8080) continuously, and if it fails it'll restart the server using an npm script (npm run dev).


Any idea on how to do it?

Thanks!

What I have tried:

Right now, I set up it so that a socket is listening to the port and every 10 or so seconds, it will call and get a response. If connection failed it will increment failCount variable.
I want it so that once it gets to an amount of times it failed to respond in a row (failCount >10) , then it will execute npm run dev to restart the service that hosts the website



 public void SetupServer()
{
    Process p = new Process();
    p.StartInfo = new ProcessStartInfo();
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.WorkingDirectory = "C:/project/client/";
    p.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
    p.StartInfo.Arguments = string.Format("/C \"{0}\"", "runclient.bat");
    p.StartInfo.ErrorDialog = false;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
Posted
Updated 7-Nov-22 3:02am
v4
Comments
Jean Ferre 4-Nov-22 14:11pm    
And what is the issue you are facing?
Member 15818964 4-Nov-22 14:19pm    
I can't seem to run the batch file that invokes the cmd line npm run dev. I tried System.Diagnostics.Process.Start(@"C:/project/client/runclient.bat"); and other solutions (I added above)

But nothing shows up (I know that windows service wouldn't allow access to desktop) but even when I try to get to localhost:8080 it would fail, so I assume that the server wasn't started
Jean Ferre 5-Nov-22 8:06am    
Have you tried to add call command in your bat file (call npm run dev) ?
Member 15818964 7-Nov-22 7:57am    
Yup, it's a bat file with npm run dev as the only line of code
Jean Ferre 9-Nov-22 12:14pm    
Did you try to add 'call' command in front of 'npm run dev' like this 'call npm run dev' and not just 'npm run dev'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900