Use Windows Service wrapper to quickly create a Windows service Foreword
Today introduces the use of a gadget. We all know that Windows service is a special kind of application, it has the advantage of being able to run in the background, relatively, it is more suitable for some applications that need to run without too much user intervention, which we call "service"
Writing Windows service is not difficult, especially if you have Visual Studio. But still have a lot of children's shoes feel slightly cumbersome, at the same time, if there are some other programs, we only get an EXE, or a bat, but want to let him run like a service, how to do?
The answer is that you can use one of the following tools, whose name is "Windows Service Wrapper, or WSW."
Basic use of WSW
The general steps to use are:
1. Download the latest version of WSW, put it in any location, and change it to any name you want.
2. Writing an XML file with the same name
3. Installing using the Install command
4. If you want to uninstall, use the uninstall command
Https://kenai.com/projects/winsw/pages/Home
This project creates a wrapper executable the can is used to the host any executable as a Windows service.
Download
The binaries is available here for download.
Usage
During Your development ...
- Take from
winsw.exe
the distribution, and rename it to your taste (such as myapp.exe
)
- Write
myapp.xml
(see Configuration Syntax for more details)
- Place those the files side by side If you deploy your application, because that's how to
winsw.exe
discovers its configuration.
At runtime ...
- To install a service, run
myapp.exe install
- To start a service, run
myapp.exe start
- To stop a service, run
myapp.exe stop
- To restart a service, run
myapp.exe restart
- To uninstall a service, run
myapp.exe uninstall
When there ' s a problem, these commands also the report an error message to stderr. On a successful completion, these commands does no produce any output and exit with 0.
In addition, you can also run to has it print out the current status of the myapp.exe status
service to stdout. Again, any error encountered during the processing would cause output to is reported to stderr.
All these commands with the same set of exit code to indicate its result.
Deferred File Operations
To the updating services, WINSW offers a mechanism to perform file operations before a service start up. This is often necessary because Windows prevents files from overwritten while it's in use.
To perform file operations, write a text file (in the UTF-8 encoding) at myapp.copies
(that's, it's in the same directory as But with a different file extension), and the operation add one line:
- To move a file, write "Src>dst". If the ' DST ' file already exists it'll be overwritten.
The success or failure of these operations would be recorded in the event log.
Contributions Welcome
If you is interested in joining the project, let me know. I ' m always interested inch more committers.
Configuration file Format Please refer to: https://kenai.com/projects/winsw/pages/ConfigurationSyntax
An example of me is as follows
After running the install, you can see this service in Services.msc
Related knowledge
In the Windows system, there is a special kind of service, they are started with a special program (SVCHOST), as follows
So, what's the matter? Interested children's shoes can refer to http://www.howtogeek.com/howto/windows-vista/what-is-svchostexe-and-why-is-it-running/
On the Linux system, there is also a similar tool (and more powerful), supervisor, interested children's shoes can refer to
Http://supervisord.org/introduction.html
Quickly create a Windows service using Windows Service Wrapper