因為項目的一些變化和原因,需要把數據處理的一個后台程序創建為一個windows服務,運行以下命令能創建成功:
sc create "MyApp Service Name" binPath= "D:/MathxH/Project/SocketS
ervice/trunk/MyApp/Win32/Release/MyApp.exe" start= auto
但是因為我的App程序是非服務(non-service)可執行程序,所以在讓它運行的時候,卻失敗了,拋出以下錯誤:
error 1053: the service did not respond to the start or control request in a timely fashion
追溯其原因,原來是需要程序,寫成一個服務可執行程序才能正確運行。
當然,也有一些工具可以讓非服務程序作為windows service來運行,比如NSSM,http://nssm.cc/ 它是開源的。由於項目時間原因,本人不想花時間研究它的實現源碼了,而且這工具就一個exe,沒其他冗余的依賴bin。所以直接可以與項目exe捆綁在一起,作為安裝時的一個輔助工具。
該工具基本的命令行:
nssm install "Service Name" "exe file path"
nssm remove "Service Name"
nssm start "Service Name"
nssm stop "Service Name"
nssm restart "Service Name"
references:
https://msdn.microsoft.com/en-us/library/40xe80wx(v=vs.80).aspx
http://stackoverflow.com/questions/3582108/create-windows-service-from-executable
https://code.msdn.microsoft.com/windowsapps/CppWindowsService-cacf4948/sourcecode?fileId=21604&pathId=2141735795