Custom Windows 2003 Services - windows

I have an .exe I want to start when the Win2k3 server boots. Does .exe needs to be setup as a custom service. If so, what is the process to setup the custom service?

I assume you don't really mean at boot time (you would have to write a driver service for that) but rather at user mode system start up.
If your process is uncomplicated you could just set it up as a scheduled task - with the trigger set to system start. If it needs to interact with the system more, i.e. needs to be paused, needs to be shutdown, warned of system events such as power events or shutdown, etc. then you should probably look to convert it to a Win32 service.
If you do need to convert to a service then start reading here and then continue with something like Richter's or Miller's books until you really understand what you are doing. Then write your service application.

If you want to create that service manually, you can use sc.exe for the job. However that exe must be capable to run as service of course.

XYNTservice a simple service that can start pretty much any program as a service.
http://www.codeproject.com/KB/system/xyntservice.aspx

Related

Stop RavenDB Service running when I don't need it

I've installed the RavenDB Service on my machine. I find it's quite memory hungry and it's on a computer i use for a lot more than developing on RavenDB, so I don't really want it running all the time (I don't want it starting the service on startup).
My current approach is to run msconfig.exe and disable the service. I will then re enable it when I need RavenDB, which (annoyingly) requires a restart. I don't see RavenDB in startup tasks, so disabling it there doesn't seem to be an option.
This question may turn out to be a simple windows service type question (if so, sorry, I should have posted this on superuser), but it may be the case that more adept configuration of RavenDB is called for.
Just like any other service, you can use the services management console to start, stop, and configure startup preferences.
Run services.msc or search for "Services" from your start screen.
You can use:
Raven.Server.exe /start
Raven.Server.exe /stop
Alternatively, run it in console mode, rather than in service mode.

Is there a way to monitor a Windows service and alert people when it hangs/stops?

We have a service running on a Windows Server 2003 machine. This service watches a particular folder on an FTP server, and when files appear there, it invokes one of a few different executables to process them.
I've been asked to find a way for staff to be alerted in some way when this service hangs or stops.
Can anyone suggest anything with just this much information? If not, what else would you need to know?
Seems we could write ANOTHER service to watch THIS service, but then there's a chance THAT one would stop ... so we haven't resolved anything.
About the only thing that I know if is writing another application or service that monitors if that service is running; something like that shouldn't have any unexpected behavior and stop, hopefully.
Another thing to do is go to the service in Windows, go to its properties, and then go to recovery options. From here, you can set the behavior of a service if it is to fail. The options in Windows 7 are to restart the service or computer, or run a program. This program could send some sort of notification. However, I don't know if any or all of these options exist in Server 2003. This would also not likely work if the service were to just hang, but a service watching it probably wouldn't either.
Also, if you have the source code, you can override some of the service-related methods such as OnStop() (for C#) to send a notification, but I don't believe this works with a failure.
My personal choice would be to set the recovery options just to restart the service on failure, unless it repeatedly fails, which there is also an option for. But just do what you think will work best for you; there isn't really a fail-safe method to do it.
UPDATE:
I did check, and Server 2003 does indeed have the same recovery options in the service manager. As the guys said above, you can deal with that, but it is only in C++ from what I have seen; there is also a command prompt way to do it:
sc failure [servicename] reset= 0 actions= restart/60000
I found that command here and you can look at it more in its MSDN documentation. You could call this command from C# or other languages if you are not using C++, or use it directly from the command prompt if you do not have the source code.
Use ChangeServiceConfig2() to define Failure Actions for your service. You could use that to invoke an external command to issue the alert (or do pretty much anything else you want) if the service terminates unexpectedly.
The SCM (the component which handles services) has built-in auto-restart logic that you can take advantage of to restart your service, as necessary. Additionally and/or alternatively, you can configure 'custom actions' to be associated with a failure of the service - the custom action can include launching a program of your own, which could then log the failure, and perhaps manually restart your service.
You can read more about such custom actions on MSDN by looking at the documentation of the structure used to configure such actions: SERVICE_FAILURE_ACTIONS. Once you fill that structure, you notify the SCM by calling the ChangeServiceConfig2 function.
Please don't ask "well, what happens if my failure handler program crashes" :)

Error 1053: The service did not respond to the start or control request in a timely fashion

I have an executable that I would like to set to run as a service. Using the sc.exe tool provided by windows (see KB article here: http://support.microsoft.com/kb/251192), I successfully "registered" the service. However, when I go to the Service Management Console (Start->Run->services.msc) and start the service, I get the following error:
Error 1053: The service did not respond to the start or control request in a timely fashion.
After reading around my initial impression is that service executables have to conform to an API, and that among the required functions/methods a service has to respond to are a start/stop/restart command. However, this seems to defeat the whole point of the sc.exe tool which is advertised with the ability to turn any executable into a service.
Can anyone shed some light on this for me?
This is the code you are looking for:
sc create SERVICENAME binPath= "cmd /c c:\programlocation\program.exe"
It will not rid you of error 1053, but at least this will apply to the console (CMD) who already did the job of running the app in the background (check task manager to confirm).
Take a look at Topshelf-project which turns many arbitrary executables into services.
When you reference Topshelf, you can run your exe from a command-line, or install it into Windows Services with:
your.exe install
Even with topshelf registered, you would still want to have a logging facility -- such as log4net -- to monitor bootstrapping activities that could prevent it from launching in a timely fashion.
One very likely cause of this behavior is the application that is started is displaying a request for interaction with the end user (messagebox, input prompt, licensing dialog, etc). We have run into this more than once. This usually happens with applications that aren't designed to run as services. You could try running the service as Local System and check the Allow service to interact with desktop checkbox.
The other possibility is that the user the service is configured to run as doesn't have access to resources (disks, databases, etc). If the process works correctly interactively, then try changing the service's user credentials to your user and see if that gets you any further.
Update
Apologies, I assumed the question was regarding srvany which is the application you are looking for.

How to code the OnStart function for windows service program?

I created a service in Visual C++. Installed it using instsrv.exe and srvany.exe.
When I check at the Services applet in Control Panel, the service is registered. But when I want to start it, this message appears:
the service on Local Computer started and then stopped. Some service stop automatically if they have no work to do, for example the Performance logs and Alert Service.
I just have this code inside my OnStart function:
(void)system("C:\ReleaseExe\Example1Server.exe");
I my code correct? Or is there any other way to code it to make sure that the service can be in running state?
Assuming that exe you are starting up is actually your service, this is the wrong way to do it.
The Windows Service Control Manager (SCM) actually calls into your service's EXE via defined entry points - you need to provide the logic to do what your particular service needs in those entry points, including notifying the SCM of state changes in your service (STOPPED-> RUNNING, RUNNING->STOPPED, and so on).
Read the background info on MSDN, starting here, for clarification of what you must provide. Note that OnStart is specific to implementing Services in managed code, you won't be doing it that way in Visual C++. Perhaps C# would be an easier route?

How does a windows service set off an application at a standard interval?

A consultant setup a windows service to run a application. The application is supposed to run every 15 minutes. The application is not running at all and the service appears to be running fine.
I am not familiar with how an application will run at a standard interval when running as a service.
The service uses the SRVANY.EXE tool.
Any 'consultant' that sets up a service to run using SRVANY.EXE should be fired. SRVANY is an unfortunate hack that should have been retired a decade ago; it should never be used in a production environment.
If the only purpose for the service is to run the app on a schedule then it shouldn't exist at all. Run the app as a Scheduled Task. If it has other functionality then rewrite it as a real service. If it is reasonably well written it should be a fairly easy conversion.
There are many potential issues with your application.
SRVANY.EXE turns any application into a Windows Service. If that application ever asks for user input, it will hang. You will want to confirm that the application running as a service does nothing more than start the other application.
You should also be able to run the "starter" application manually, outside the Windows Service. If it still doesn't work as it should you know it's not related to being run as a service.
To add to the other answers: see KB137890 on what SRVANY.EXE actually does and how to find out what application it is running.
It seems to me that you would be better off (if you can) setting up a scheduled task that runs every the application every 15 minutes if you can.
I'm not sure if this is correct, but I believe one way of a serivce running an application is merely to have a thread within OnStart and set it to run the application on an invertal of 15 minutes.

Resources