-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresetar-jboss.cpp
62 lines (54 loc) · 1.93 KB
/
resetar-jboss.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <windows.h>
#include <winbase.h>
#include <winnt.h>
#include <winsvc.h>
#include <string>
#include <thread>
#include <chrono>
#include <future>
using namespace std;
/*
Stop a windows Service with the name JBOSS and then start again after 45 seconds, if the service is already stopped, start it
*/
int main()
{
SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL) {
cout << "OpenSCManager failed (" << GetLastError() << ")" << endl;
return 1;
}
SC_HANDLE schService = OpenService(schSCManager, "JBOSS", SERVICE_ALL_ACCESS);
if (schService == NULL) {
cout << "OpenService failed (" << GetLastError() << ")" << endl;
return 1;
}
SERVICE_STATUS_PROCESS ssp;
DWORD dwBytesNeeded;
if (!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded)) {
cout << "QueryServiceStatusEx failed (" << GetLastError() << ")" << endl;
return 1;
}
if (ssp.dwCurrentState == SERVICE_STOPPED) {
cout << "Servico JBOSS ja esta paralizado, reiniciando novamente..." << endl;
// start the service
if (!StartService(schService, 0, NULL)) {
cout << "Iniciar o servicos do JBOSS falhou. (" << GetLastError() << ")" << endl;
return 1;
}
cout << "Servico JBOSS inicializado" << endl;
return 0;
}
if (!ControlService(schService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp)) {
cout << "ControlService falhou (" << GetLastError() << ")" << endl;
return 1;
}
cout << "Servico JBOSS paralizado" << endl;
this_thread::sleep_for(chrono::seconds(45));
if (!StartService(schService, 0, NULL)) {
cout << "StartService falhou (" << GetLastError() << ")" << endl;
return 1;
}
cout << "Servico JBOSS inicializado" << endl;
return 0;
}