简体   繁体   English

从相同的Windows服务重新启动Windows服务

[英]restart windows service from the same windows service

I am trying to restart a windows service from the same windows service with this code: 我正在尝试使用以下代码从相同的Windows服务重新启动Windows服务:

            var psi = new ProcessStartInfo("net.exe", "stop " + serviceName);
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.UseShellExecute = true;
            psi.WorkingDirectory = Environment.SystemDirectory;
            var st = Process.Start(psi);
            st.WaitForExit();

            psi = new ProcessStartInfo("net.exe", "start " + serviceName);
            psi.UseShellExecute = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.WorkingDirectory = Environment.SystemDirectory;
            st = Process.Start(psi);
            st.WaitForExit();

It is not working but is this even possible from the same service? 它不起作用,但是即使通过同一服务也可以做到吗?

If it is possible then can anyone suggest why this does not work? 如果有可能,那么谁能建议为什么这不起作用?

It doesn't work because once you reach the middle, execution stops in since both parent and child process exit: 它不起作用,因为一旦到达中间位置,由于父进程和子进程都退出,因此执行会停止:

/* Running */            var psi = new ProcessStartInfo("net.exe", "stop " + serviceName);
/* Running */            psi.WindowStyle = ProcessWindowStyle.Hidden;
/* Running */            psi.UseShellExecute = true;
/* Running */            psi.WorkingDirectory = Environment.SystemDirectory;
/* st stops svc */       var st = Process.Start(psi);
/* both exit */          st.WaitForExit();

/* never reached */      psi = new ProcessStartInfo("net.exe", "start " + serviceName);
/* never reached */      psi.UseShellExecute = true;
/* never reached */      psi.WindowStyle = ProcessWindowStyle.Hidden;
/* never reached */      psi.WorkingDirectory = Environment.SystemDirectory;
/* never reached */      st = Process.Start(psi);
/* never reached */      st.WaitForExit();

Configure the service in windows to restart itself on failure and then do an unclean exit as @StevieB suggested. 在Windows中配置服务以在失败时自动重启,然后按照@StevieB建议执行不干净的退出。

Configure the service to restart itself with a 1000 ms delay on failure: 将服务配置为在失败时延迟1000毫秒以重新启动自身:

sc failure serviceName reset= 60 actions= restart/1000

I don't believe there's a clean way of doing this. 我不认为这样做有一种干净的方法。 Once you stop the service, the process is no longer there to start it. 一旦停止服务,就不再需要启动该过程。

One slightly dirty solution: You could configure the service to restart on error (eg at installation/registration time). 一种稍微肮脏的解决方案:您可以将服务配置为在错误时重新启动(例如,在安装/注册时)。

Then when you want to restart programmatically have your service exit with an error eg by calling: 然后,当您要以编程方式重新启动时,服务会退出并显示错误,例如通过调用:

Environment.Exit(-1)

or 要么

Environment.FailFast("Service stopped because...")

Note: If there is a cleaner way of doing this programmatically the account the service is running under would require permissions to be able to restart the service. 注意:如果有更干净的方法以编程方式执行此操作,则该服务在其下运行的帐户将需要权限才能重新启动该服务。

You have a deadlock. 你陷入僵局。 net is waiting for your service to end, and you are using st.WaitForExit() to wait for net to end. net正在等待服务结束,并且您正在使用st.WaitForExit()等待net结束。

Eventually net gives up waiting and closes. 最终, net放弃等待并关闭。 Then you call net again. 然后,您再次致电net But your service never exited in the first place, so the second net invocation sees the service is already started and does nothing. 但是您的服务从来没有退出过,因此第二次net调用会看到该服务已经启动并且什么也不做。

Finally you return the the main service event loop and see the stop request, and your service exits. 最后,您返回主服务事件循环并查看停止请求,然后服务退出。

You should be able to write a batch file with the above commands, invoke it, and return to the event loop. 您应该能够使用上述命令编写一个批处理文件,调用它,然后返回到事件循环。 Then the first net will wait until the service stops, and the second net will start it again. 然后,第一个net将等待直到服务停止,第二个net将再次启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM