How to Run your Own Program as a Service in Windows

written by: Leon Gualee; article published: year 2006, month 12;


In: Root » Computers and technology » Microsoft OS family » How to Run your Own Program as a Service in Windows

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

Developing a Windows service program requires a fair bit of effort and programming skill. In addition to writing code to do whatever job the service has to do, you must add extra code that lets the service program communicate with the Windows Service Manager, so that the service can be started, stopped, and can communicate its status and dependencies to the manager.

Still, in some cases it's nice to be able to have a program run when Windows boots up and have it stay running 24x7, whether you're logged on or not, and no matter what else is going on. And, in fact you can do this, using a utility program from the Windows 2000 Resource Kit that runs a program of your choice as if it were a service; the utility takes care of communicating with the Service manager, and runs your program using a specified command-line when the service manager starts it up. Your program can be a Windows application, command-line program, batch file, Windows Script Host script, database application, or a program written in another scripting language such as Perl. I've used this method to create a web server database back-end written in the FoxPro database language, and an interface to the SpamAssassin spam-filtering program written in Perl.

A program that is to act as a service needs to function with no interaction from the keyboard, mouse, or screen. To communicate with the outside world, it can create and listen on network sockets, or it can scan a specified directory every so many seconds for the appearance of files. It should not run in an endless loop waiting for work to do, or it will slow performance of your computer. Instead, it should use Windows synchronization tools like Events, or at least it should "sleep" for periods of time in order not to consume any CPU power when it's idle. Test the program from the command line to be sure it works before trying to run it as a service.

Then, when you are ready to install it as a service, follow these steps:

Caution: Running a program as a service this way has some risks. In fact, the Windows AntiSpyware program reports the presence of Srvany as a risk every time it runs. If a hacker replaces the program that the service manager starts up, the bogus program will run with whatever privileges the service would have run with. You should follow the steps listed here to ensure that your service is safe from hacking.

  • 1. Log on as a Computer Administrator.
  • 2. Download the Windows 2000 Server or Windows Server 2003 Resource Kit Tools package from microsoft.com, as discussed in Appendix A. Install the package.
  • 3. Click Start, All Programs, and find the new Resource Kit Tools entry. Open the Resource Kit Tools help file, and search for srvany. On the srvany page, there are several links to other pages that you'll need to read: Installing Srvany, Running an Application as a Service, Starting and Stopping a Service, and Srvany Notes. You may want to print these documents.
  • 4. Create a special user account to be used just for your service, and set a password for it.
  • 5. Create a special folder for the service's files on a drive that is formatted with the NTFS file system (do not use a FAT-formatted disk). For these instructions, I'll assume that the folder is C:\myservice.
  • 6. In the Resource Kit tools folder under Program Files, locate files instsrv.exe and srvany.exe. Copy instsrv.exe, srvany.exe, the program file(s) for your service, and any data files it needs to the service's folder (c:\myservice in this example).
  • 7. Set NTFS permissions for this folder so that only Administrator and the new user account have access to it. Use the Advanced button to reset permissions on all objects in the folder. (Simple File Sharing must be disabled in order to set permissions.)
  • 8. Open a command prompt window and change to the service's folder, for example, with cd /d c:\myservice.
  • 9. Choose a name for your service; it must be different than any other service, and should describe in a word or two what the service does.
  • 10. Follow the instructions for installing srvany as a service, and for creating the Application and AppParameters values that specify your program and its command line parameters. Table 6.4 lists the correct Application values for various types of programs; replace the filename in italics with the appropriate filename for your program.
Application and AppParameters Values for Various Types of Service Programs
Program Type Registry Values
Standard .EXE
AppParameters any needed parameters
Application myprogram.exe
Batch file
AppParameters /c mybatch.bat
Application c:\windows\system32\cmd.exe
Script
AppParameters myscript.vbs
Application c:\windows\system32\cscript.exe
  • 11. Create the AppDirectory value and set it to the full path of your service's folder.
  • 12. When the service has been installed, open the Services management tool, locate your service, and use the Log On tab to specify that it's to run under the special user account.

Now, you should be able to start the service and press Ctrl+Alt+Del to see that the associated program appears in the Windows Task Manager's Processes display. If it doesn't, it may be exiting prematurely, or there may be a problem starting the service. In the latter case there should be a record in the Event log.

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article