Forcing restart of service

Forcing restart of service

Post by c2hhcm9uZ » Thu, 01 Dec 2005 02:40:08


hi
I apologize in advance if this is the wrong newsgroup to post to - I saw a
service question posted on this site...
I cannot get my service to restart on an error, though it will restart if I
delete it from a task manager. (I'm on XP SP2 and the code is written in
C++.)The calls I make to ChangeSErviceConfig2() to force a restart return
success (I've pasted in the code below). What do I need to do specifically to
cause the restart once I determine I have an error that requires a restart?
How do I tell the SCM/OS that I need to be restarted?
After the service is up and running, I am trying to force the error
condition. I tried setting the dwWin32ExitCode to -1 and then set the service
status (SetServiceStatus) to SERVICE_STOPPED. I also tried setting the
dwWin32ExitCode to 0, the dwServiceSpecificCode to -1 and set the service
status to SERVICE_STOPPED. Finally, I tried having my main routine return -1.
While I can get my service to be removed from the running processes (it's not
listed in Task Manager), it won't restart.
Here's the code:
#ifdef UNICODE
PFNCSC pfnChangeSvc = (PFNCSC)GetProcAddress(hInst,
"ChangeServiceConfig2W");
#else
PFNCSC pfnChangeSvc = (PFNCSC)GetProcAddress(hInst,
"ChangeServiceConfig2A");
#endif

SERVICE_FAILURE_ACTIONS sfa;
SC_ACTION sa;
memset( &sfa, 0, sizeof(sfa));
memset( &sa, 0, sizeof(sa));
sfa.dwResetPeriod = INFINITE;
sfa.lpCommand = NULL;
sfa.lpRebootMsg = NULL;
sa.Type = SC_ACTION_RESTART; // restart the service
sa.Delay = SERVICE_FAILURE_ACTION_RESTART_DELAY; // set to 1000
sfa.lpsaActions = &sa;
sfa.cActions = 1;
fRet2 = pfnChangeSvc( hService, SERVICE_CONFIG_FAILURE_ACTIONS, &sfa );
if ( fRet2 )

The recovery tab for the service shows:
First failure: Restart the Service
Second failure: Restart the Service
Subsequent failures: Restart the Service
I do not specify a program to run.
Any suggestions would be helpful. Thanks in advance.
 
 
 

Forcing restart of service

Post by Kellie Fit » Thu, 01 Dec 2005 03:37:40

Hi,

Have you tried to open up the service applet (service.msc), then
bring up the property sheet and fill in the options on the recovery
tab ?

Kellie.

 
 
 

Forcing restart of service

Post by c2hhcm9uZ » Thu, 01 Dec 2005 03:58:12

Thank you for the reply. The property sheet is already set up
programmatically. I listed the settings at the end of my posting, which
indicate they were all set to indicate a restart (but perhaps that wasn't
clear).

I was able to get my service to restart to occur by doing an ExitProcess(-1)
before making the call to set SERVICE_STOPPED (SetServiceStatus) with
dwServiceCode = ERROR_SERVICE_SPECIFIC_ERROR and dwSErviceSpecificCode = -1.
I also tried calling it after setting SERVICE_STOPPED, but that didn't work.

While calling ExitProcess(-1) works, I think I should be reporting the error
codes to SCM. Maybe I'm naive, but I think I should be reporting the error
codes to the SCM and based on it receiving those error codes, it decides to
restart my service.

Suggestions on the proper way to force a restart would be helpful.

thanks
 
 
 

Forcing restart of service

Post by Kellie Fit » Thu, 01 Dec 2005 04:56:08

Hi,

Well, one thing you could do would be to create an application
that function as a watchDog, that sends interrogate messages to
the service process at some time interval, If the watchDog does
not receive any replies back then the service is not running, so
the watchDog should restart the service process accordingly.

Kellie.