Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

How to run a program as a Service

- - - - -

  • Please log in to reply
18 replies to this topic

#1
tain

tain

    Cyber Ops

  • Super Moderator
  • 3,685 posts
  • Joined 24-September 05
  • OS:none specified
  • Country: Country Flag

Donator

I wanted to setup some programs to run as Windows Services but found information on the subject to be scattered around everywhere. So this post is basically for consolidation and clarification but is not focused on unattended installations.

What is a Service?
A Windows Service is automatically started when your system boots. There is no need to logon to the system. Services also are not affected by people logging off. Services automatically recover from program crashes and Standby and Hibernation modes. Windows 9x/ME do not have a Service facility. Installing software as a Service requires Administrator permissions.

What are the options for running a program as a Service?
  • sc.exe
    This is Microsoft software and is installed by default on XP and 2003. It is probably the best and easiest method but it won't work if the target executable was not programmed to run as a Service. This rules it out for what many people are trying to do since most programs that are designed ro run as a Service have that option included somewhere in the application. sc.exe is useful for the Unattended crowd, though.

    The syntax for sc.exe is worth noting. This will NOT work:

    sc create YourServiceName binPath="c:\Program Files\directory\appname.exe"

    But this WILL work (note the space after the =):

    sc create YourServiceName binPath= "c:\Program Files\directory\appname.exe"

    Two other useful options, with the same syntax as above, are:
    type= <own|share|interact|kernel|filesys|rec>
    	  (default = own)
    start= <boot|system|auto|demand|disabled>
    	  (default = demand)
    You can use these commands for more details:
    sc /?
    sc create /?
  • instsrv.exe and srvany.exe
    This is Microsoft software but was published as part of the Resource Kits. You can download the files here or here. Thanks to tacktech.com for providing the fileset.

    Instrsrv.exe installs the Service but srvany.exe is the real gem here as it is a wrapper that allows any software to be run as a Service. This method worked great for me but does require manual registry edits.

    Here is a quick summary of the steps invloved but you can find more complete instructions at Microsoft's KB page or TackTech's page with great screenshots.
    • Unpack the fileset to a folder of your choice. srvany.exe needs to remain on your system for this method to work, so put it somewhere like Program files where you won't mind leaving it around. I use C:\Program Files\services in this example.
    • Install the Service with a command like this:
      C:\Program Files\services\Instsrv.exe YourServiceName C:\Program Files\services\Srvany.exe
    • Using a registry editor, find this key:
      HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\YourServiceName
    • Add a new sub-key called Parameters.
    • In the Parameters key, create a new string value named Application.
    • Set the value data of Application to the full path of the executable file that you want the service to launch. For example, C:\WINNT\notepad.exe.
    • Using the Services console, find your new service and make any necessary property edits. This dialog will allow you to set the startup type, logon method, and program failure options.
    One problem I ran into with this method was task tray entries. Some programs won't work if their task tray entry fails. If the software you want to run wants to put an icon in the task tray you can try enabling "Allow service to interact with the desktop" on the "Log On" tab of the Service properties. If that doesn't work you could also try disabling the task tray entry for the program.
  • Launcher Service
    Camarade_Tux recommends Launcher Service. It is free and open source and looks very flexible and useful. This program has several unique features:
    • Start or stop a program based on network connectivity
    • External program can be run before or after execution
    • Delete *.tmp and *.pid files from the program's startup folder before execution
  • WinServ

    WinServ is a utility that can create an NT service that runs any application. When the application exits, the service becomes stopped.

    I did not try this program but included it here since it is free, open source and looks useful.
  • Non-free options
    There are quite a few shareware and payware options. I didn't try any of them but I hear that FireDaemon works well for a lot of people.
References
  • http://support.microsoft.com/kb/251192
  • http://support.microsoft.com/kb/q137890
  • http://www.tacktech.com/display.cfm?ttid=197
  • http://www.msfn.org/board/index.php?showtopic=72799
  • http://www.msfn.org/board/index.php?showtopic=40581
  • http://www.msfn.org/board/index.php?showtopic=62482
  • http://www.codeproject.com/system/serviceskeleton.asp



How to remove advertisement from MSFN

#2
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,852 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag
Very interesting post! :)

I may add (Freeware for non commercial use):
NTWRAPPER
http://www.duodata.d...apper/index.htm

Codeproject (Sourcecode only) - any taker to compile it?:
XYNTSERVICE
http://www.codeproje...xyntservice.asp

jaclaz

#3
tain

tain

    Cyber Ops

  • Super Moderator
  • 3,685 posts
  • Joined 24-September 05
  • OS:none specified
  • Country: Country Flag

Donator

Thanks, jaclaz :)

NT Wrapper looks great but I can't find the differences between the Lite and Pro versions. Do you know?

The Lite version also comes with a neat app called SaferTray which helps keep wandering icons in the tray where they belong.

I looked at that other app previously but didn't include it since it is aimed at programmers instead of users. But if you look at the comments at the bottom of the page you will see the familiar name Bilou_Gateux! I wonder if he got a copy and if it is suitable for use here? :whistle:

/me PMs him

#4
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

NT Wrapper looks great but I can't find the differences between the Lite and Pro versions.

There's a chart on that page.

SMP/CPU binding only in pro version;
Lite version limited to one service/machine only
Pro version has offline help and support.

Codeproject (Sourcecode only) - any taker to compile it?:

Absolutely! I would, but there's no need for that as there's also a pre-compiled version along with the source in the zip file. If you have no account (req'd to download) and don't want to signup, then try bugmenot.
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#5
tain

tain

    Cyber Ops

  • Super Moderator
  • 3,685 posts
  • Joined 24-September 05
  • OS:none specified
  • Country: Country Flag

Donator

So there is. :blink:

I will blame this blunder on my sometimes over-eager filtering proxy. I see the table when I bypass it. Thanks!

Being limited to one service would be a deal breaker for me. How many apps do you guys think most people need to run as a service? I need at least two, but probably three.

#6
CoffeeFiend

CoffeeFiend

    Coffee Aficionado

  • Super Moderator
  • 5,399 posts
  • Joined 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Being limited to one service would be a deal breaker for me. How many apps do you guys think most people need to run as a service? I need at least two, but probably three.


Hard to tell... Personally I don't use any of these apps as Visual Studio will let you create your own services when you need one... (and even an installer for it and all - example with source code here)

But there are likely reasons to run other apps as a service like that (not that I've ever had the need - most apps that need or would benefit from this already take care of it, like web/ftp servers).

I can understand he wants to charge for it though - not that it's a very complex app or anything, but it takes time to design/code/make installer, write proper documentation, create a website for it (pay associated costs too), pay for hosting, end up having to support people (some people will ask questions - so you need email or a forum or something - and it can take a fair amount of time to answer those), maintain/bugfix/enhance it, etc. Understandable he wants to get some of that money back (or be paid for part of his time spent). So he's putting some artificial limitations on it to get their users with the most needs to pay for it.

Thing is, it's easy to create a simple app like that, and there's already some free & open source options (I bet one would find some stuff on sourceforge too). So people are mostly paying for convenience (they don't want to have to write or compile code or such things) - that and support. Someone with some knowledge and time will use something else or just "roll their own" basically.

People with simple needs will use the free version, others will either pay ~20$/PC or find something else if that's too much (but then you need to spend time looking for something else and such - not as "convenient")... Personally, I'd rather use something that I have the source for (willing to fix/enhance/modify it as req'd)
Coffee: \ˈkȯ-fē, ˈkä-\. noun. Heaven in a cup. Life's only treasure. The meaning of life. Kaffee ist wunderbar. C8H10N4O2 FTW.

#7
Zartach

Zartach

    Junior

  • Member
  • Pip
  • 91 posts
  • Joined 24-January 06
I have used vbscript in the past to create services:

CreateService.vbs
Const OWN_PROCESS = 16
Const NOT_INTERACTIVE = False
Const NORMAL_ERROR_CONTROL = 2

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")

Set objService = objWMIService.Get("Win32_BaseService")
errReturn = objService.Create("DbService", _
"NAME", _
"C:\WINDOWS\PROGRAM.EXE", _
OWN_PROCESS, _
NORMAL_ERROR_CONTROL, _
"Manual", _
NOT_INTERACTIVE, _
"NT AUTHORITY\LocalService", "")

RemoveService.vbs
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = 'SERVICE_NAME'")
For Each objService in colListOfServices
objService.StopService()
objService.Delete()
Next

I do believe that this will work only for programs that are meant to run as a service.

#8
Bilou_Gateux

Bilou_Gateux

    Powered by Windows Embedded

  • Member
  • PipPipPipPipPip
  • 768 posts
  • Joined 03-January 04

Very interesting post! :)

I may add (Freeware for non commercial use):
NTWRAPPER
http://www.duodata.d...apper/index.htm

Codeproject (Sourcecode only) - any taker to compile it?:
XYNTSERVICE
http://www.codeproje...xyntservice.asp

jaclaz


XYNTSERVICE GUI wrapper with compiled version
;)

Edited by Bilou_Gateux, 26 September 2006 - 10:18 AM.

OS Version = 5.1.2600 Service Pack 3
Platform ID = 2 (NT)
Service Pack = 3.0
Suite = 0x0140
Product Type = 1
Architecture = x86

#9
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,852 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

XYNTSERVICE GUI wrapper with compiled version


I stand (pleasantly) corrected. :thumbup

jaclaz

#10
Camarade_Tux

Camarade_Tux

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,760 posts
  • Joined 22-May 05

I have used vbscript in the past to create services:

CreateService.vbs

Const OWN_PROCESS = 16
Const NOT_INTERACTIVE = False
Const NORMAL_ERROR_CONTROL = 2

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")

Set objService = objWMIService.Get("Win32_BaseService")
errReturn = objService.Create("DbService", _
"NAME", _
"C:\WINDOWS\PROGRAM.EXE", _
OWN_PROCESS, _
NORMAL_ERROR_CONTROL, _
"Manual", _
NOT_INTERACTIVE, _
"NT AUTHORITY\LocalService", "")

RemoveService.vbs
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = 'SERVICE_NAME'")
For Each objService in colListOfServices
objService.StopService()
objService.Delete()
Next

I do believe that this will work only for programs that are meant to run as a service.


Afaik, windows DOS-style programs have a main function, graphical ones a winmain one and services have serv(ice)main or so function.
So a program needs to be designed to run as a service and moreover, services receive some specific messages (shutting down and co.) your program should handle if you want it not to simply die when you power your computer off.

[*]Launcher Service
Camarade_Tux recommends Launcher Service. It is free and open source and looks very flexible and useful. This program has several unique features:

  • Start or stop a program based on network connectivity
  • External program can be run before or after execution
  • Delete *.tmp and *.pid files from the program's startup folder before execution


... to be continued. :D

#11
kitaec

kitaec

    Newbie

  • Member
  • 18 posts
  • Joined 12-May 06
Do you think it is possible to make a program starting as a service during Unattended?

Say it's some kind of driver (dll or sys) that can act as a service after regular installation.

I think Bilou_Gateux did something similar :rolleyes:

#12
LLXX

LLXX

    MSFN Junkie

  • Banned
  • PipPipPipPipPipPipPipPipPip
  • 3,399 posts
  • Joined 04-December 05
Noone mentioned the "at xx:xx program.exe" ? This uses the task manager to run a program as a service at a specific time (and under full-priviledge System account too...)

#13
tain

tain

    Cyber Ops

  • Super Moderator
  • 3,685 posts
  • Joined 24-September 05
  • OS:none specified
  • Country: Country Flag

Donator

@LLXX: Please explain.

@kitaec: Some of the methods in this thread can be used for unattended. Look at sc.exe and the vbscript.

#14
kitaec

kitaec

    Newbie

  • Member
  • 18 posts
  • Joined 12-May 06
TAiN
Thank you. But doesn't sc.exe applies to .EXE files? And if there is a .SYS file (say - system driver)? And there are two .INF files with it which install and specify this .SYS as a service. But they don't make it to start like a service :(

Vbscripts can't be used for some reason :)

#15
Ctrl-X

Ctrl-X

    Advanced Member

  • Member
  • PipPipPip
  • 444 posts
  • Joined 29-August 06
Excellent topic; should be made sticky IMHO :)

#16
tain

tain

    Cyber Ops

  • Super Moderator
  • 3,685 posts
  • Joined 24-September 05
  • OS:none specified
  • Country: Country Flag

Donator

But doesn't sc.exe applies to .EXE files?

Yes. What are you trying to do?

Just stumbled onto this page today:
Quick Start Multiple Services using Batch and Net
Nothing new, but demonstrates usage of the batch commands.

#17
axelabs

axelabs
  • Member
  • 1 posts
  • Joined 13-February 14
  • OS:Server 2008 x86
  • Country: Country Flag

srvany.exe worked for me.  A few things... I specifically had to use instsrv.exe and not the builtin sc.exe command to get it to register properly.  Also, I downloaded the Resource Kit directly from microsoft and then extracting only the two utilities by first extracting it to a temp location and then installing the msi into a directory.

rktools.exe /T:C:\Temp\rktools /C
msiexec  /qn /i rktools.msi RKTOOLS="C:\Temp\rktools\Tools


#18
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,852 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

Just for the record, nowadays I would rather use NSSM:

NSSM - the Non-Sucking Service Manager

http://nssm.cc/

as it worked for me nicely more than once :yes:

 

jaclaz



#19
TELVM

TELVM

    Advanced Member

  • Member
  • PipPipPip
  • 305 posts
  • Joined 09-February 12
  • OS:Windows 7 x64
  • Country: Country Flag

Just for the record, nowadays I would rather use NSSM:

NSSM - the Non-Sucking Service Manager

http://nssm.cc/

as it worked for me nicely more than once :yes:

 

jaclaz

 

Wow highly interesting! Does NSSM work in XP and Se7en Jaclaz?

 

 

EDIT- Never mind: "... nssm should work under Windows 2000 or later. Specifically, Windows 7 and Windows 8 are supported. 32-bit and 64-bit binaries are included in the download ..."


Edited by TELVM, 20 February 2014 - 08:46 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users