Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup my server exe keep running

Tags:

c#

windows

I have a server process which is deployed now, and I want to keep my server running. In case, if that process crashed by some means, either some exception or any other technical flow, then I need it to be restart again.

For this, I have created another process which keep checking(e.g, every 2 minutes) in processes list for this particular process name, and in case, if not found, it will start that process.

I have implemented this solution, but I know its not a good idea, I believe there must be some better ideas for this job. Can you help me is it best approach, or suggest some better one?

For some limitations, I could not deploy my server process as windows service, its a windows form application.

Thanks.

like image 737
M_Idrees Avatar asked Mar 14 '26 23:03

M_Idrees


2 Answers

You didn't post any code, but you mentioned "keep checking the processes list every two minutes". I think a better solution would be to have your watchdog process just be the parent process. It would call CreateProcess to launch your EXE as a child process. Then do a WaitForSingleObject on the child process handle. As soon as WaitForSingleObject returns (signaling that the child process has exited), it would just restart it.

The .Net equivalents of CreateProcess and WFSO are Process.Start and Process.WaitForExit.

like image 110
selbie Avatar answered Mar 17 '26 13:03

selbie


As you say, the solution is far from optimal. Windows services has automatic restart.

In your case I would start your watch dog process from windows task scheduler to avoid problems if your watch dog dies.

like image 34
Albin Sunnanbo Avatar answered Mar 17 '26 13:03

Albin Sunnanbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!