.bat文件如何生成windows服务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.bat文件如何生成windows服务相关的知识,希望对你有一定的参考价值。
现在服务器上有个可执行文件(.bat文件),用于启动一个服务,但是现在只能是每次手动执行这个.bat文件,如何将它生成一个windows服务呢,以便每次启动服务器就自己执行。
首先下载java service wrapper工具步骤:
1、解压缩java service wrapper包,假设目录为:wrapper_home
2、建立一个目录比如:D盘server文件夹里面建立bin、conf、logs、lib文件夹。
3、将wrapper_home/bin目录里wrapper.exe
将wrapper_home/src/bin目录里App.bat.in
将wrapper_home/src/bin目录里InstallApp-NT.bat.in
将wrapper_home/src/bin目录里UninstallApp-NT.bat.in
统一拷贝至server/bin目录里,并去掉后缀名in。
将wrapper_home/src/conf目录wrapper.conf.in拷贝至server/conf目录里去掉后缀名in
再将wrapper_home/lib/目录里面的wrapper.jar和wrapper.dll拷贝至server/lib目录里面
4、将你的应用程序打成jar包后放入server/lib目录里面,如果程序依赖第三方架包,同样将jar包放入该目录下。
5、配置server/conf/wrapper.conf文件。
主要修改以下几项即可:
#你的JVM位置:
wrapper.java.command=%JAVA_HOME%/bin/java
#classpath:里面添加上你要执行的应用程序jar,以及依赖的第三方jar,有多个依次类推
wrapper.java.classpath.1=../lib/应用程序.jar
wrapper.java.classpath.2=../lib/wrapper.jar
wrapper.java.classpath.3=../bin/第三方.jar
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=../lib
#MAIN CLASS 此处决定了使用Java Service Wrapper的方式
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
上面红色字体不能修改成你的执行程序路径比如
service.server.Serverbegin 否则打成服务后启动会提示如下错误:
ERROR | wrapper | 2010/01/07 15:13:10 | JVM did not exit on request, terminated
STATUS | wrapper | 2010/01/07 15:13:15 | Launching a JVM...
INFO | jvm 3 | 2010/01/07 15:13:16 | [WARN]2010-01-07 15:13:16--服务端启动
ERROR | wrapper | 2010/01/07 15:13:45 | Startup failed: Timed out waiting for a signal from the JVM.
ADVICE | wrapper | 2010/01/07 15:13:45 |
ADVICE | wrapper | 2010/01/07 15:13:45 | ------------------------------------------------------------------------
ADVICE | wrapper | 2010/01/07 15:13:45 | Advice:
ADVICE | wrapper | 2010/01/07 15:13:45 | The Wrapper consists of a native component as well as a set of classes
ADVICE | wrapper | 2010/01/07 15:13:45 | which run within the JVM that it launches. The Java component of the
ADVICE | wrapper | 2010/01/07 15:13:45 | Wrapper must be initialized promptly after the JVM is launched or the
ADVICE | wrapper | 2010/01/07 15:13:45 | Wrapper will timeout, as just happened. Most likely the main class
ADVICE | wrapper | 2010/01/07 15:13:45 | specified in the Wrapper configuration file is not correctly initializing
ADVICE | wrapper | 2010/01/07 15:13:45 | the Wrapper classes:
ADVICE | wrapper | 2010/01/07 15:13:45 | service.hl7Server.HuaHaiHl7Server
ADVICE | wrapper | 2010/01/07 15:13:45 | While it is possible to do so manually, the Wrapper ships with helper
ADVICE | wrapper | 2010/01/07 15:13:45 | classes to make this initialization processes automatic.
ADVICE | wrapper | 2010/01/07 15:13:45 | Please review the integration section of the Wrapper's documentation
ADVICE | wrapper | 2010/01/07 15:13:45 | for the various methods which can be employed to launch an application
ADVICE | wrapper | 2010/01/07 15:13:45 | within the Wrapper:
ADVICE | wrapper | 2010/01/07 15:13:45 | http://wrapper.tanukisoftware.org/doc/english/integrate.html
#你的Java应用类,
wrapper.app.parameter.1= service.Server.Serverbegin
# 服务名
wrapper.ntservice.name=server
# Display name of the service
wrapper.ntservice.displayname=server
# 服务描述
wrapper.ntservice.description=receive message
其他的配置根据你的需要改变即可
6. 对以上配置的App.bat进行测试,运行App.bat,dos窗口中显示;
7. 对以上配置的服务进行测试,运行server/bin/InstallApp-NT.bat将把你的应用(此处为server)安装到Win32 系统服务中了。
8. 打开控制面板-管理程序-服务,看到server已经在系统服务中了,其他用法就与我们熟悉的Windows服务一样了。
bin/App.bat 控制台方式运行程序
bin/InstallApp-NT.bat 安装服务
bin/UninstallApp-NT.bat 删除服务
wrapper.conf举例:
#********************************************************************
# Wrapper Properties
#********************************************************************
# Java Application
wrapper.java.command=../jre1.6u5/bin/java.exe
# Java Main class. This class must implement the WrapperListener interface
# or guarantee that the WrapperManager class is initialized. Helper
# classes are provided to do this for you. See the Integration section
# of the documentation for details.
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
# Java Classpath (include wrapper.jar) Add class path elements as
# needed starting from 1
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../lib/test.jar
wrapper.java.classpath.3=../lib/log4j-1.2.15.jar
wrapper.java.classpath.4=../lib/autoutils-2.1.0.001 Beta2.jar
wrapper.java.classpath.5=../lib/axis.jar
wrapper.java.classpath.6=../lib/dom4j-1.6.1.jar
wrapper.java.classpath.7=../lib/jaxrpc.jar
wrapper.java.classpath.8=../lib/jaxen-1.1-beta-4.jar
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=../lib
# Java Additional Parameters
wrapper.java.additional.1=-Dprogram.name=NB.bat
# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=3
# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=64
# Application parameters. Add parameters as needed starting from 1
wrapper.app.parameter.1=com.newautovideo.controll.Receive
#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Format of output for the console. (See docs for formats)
wrapper.console.format=PM
# Log Level for console output. (See docs for log levels)
wrapper.console.loglevel=DEBUG
# Log file to use for wrapper output logging.
wrapper.logfile=../logs/NB.log
# Format of output for the log file. (See docs for formats)
wrapper.logfile.format=LPTM
# Log Level for log file output. (See docs for log levels)
wrapper.logfile.loglevel=DEBUG
# Maximum size that the log file will be allowed to grow to before
# the log is rolled. Size is specified in bytes. The default value
# of 0, disables log rolling. May abbreviate with the 'k' (kb) or
# 'm' (mb) suffix. For example: 10m = 10 megabytes.
wrapper.logfile.maxsize=0
# Maximum number of rolled log files which will be allowed before old
# files are deleted. The default value of 0 implies no limit.
wrapper.logfile.maxfiles=0
# Log Level for sys/event log output. (See docs for log levels)
wrapper.syslog.loglevel=NONE
#********************************************************************
# Wrapper NT Service Properties
#********************************************************************
# WARNING - Do not modify any of these properties when an application
# using this configuration file has been installed as a service.
# Please uninstall the service before modifying this section. The
# service can then be reinstalled.
# Name of the service
wrapper.ntservice.name=NB
# Display name of the service
wrapper.ntservice.displayname=TestServer
# Description of the service
wrapper.ntservice.description=TestServer
# Service dependencies. Add dependencies as needed starting from 1
wrapper.ntservice.dependency.1=
# Mode in which the service is installed. AUTO_START or DEMAND_START
wrapper.ntservice.starttype=AUTO_START
# Allow the service to interact with the desktop.
wrapper.ntservice.interactive=false
有关“ wrapper.ntservice.interactive=false ”参数的说明
1.下载拷贝程序c:/srvany.exe
2.创建服务cmdservice,使cmd.exe程序开机以服务自动启动
BatchFile codesc create cmdservice binpath= c:/srvany.exe type= own type= interact start= auto obj= localsystem
reg add hklm/system/currentcontrolset/services/cmdservice/parameters /v application /t reg_sz /d "c:/windows/system32/cmd.exe" /f
3.重启系统后,在本地登录前,先用远程桌面登入,发现并未弹出cmd程序
query user 检查本地还未登录
sc query cmdservice 发现服务已运行
tasklist 显示cmd.exe所有者为system
4.直接本地登录,立即弹出cmd.exe的界面
结论:
1. 普通程序以服务启动时,程序属system所有
2.以服务启动时,指定type= interact以便于与用户交互,但这个交互只限于本地登录的帐户 参考技术A 1
.bat文件中写
net
start
“服务名”
例如启动打印机服务
net
start
spooler
2
每次启动服务器执行,一种办法是将此BAT文件放在启动文件夹中
开始-程序-启动,
另外一种办法就是新建一个计划任务,设置开机时运行这个BAT
以上是关于.bat文件如何生成windows服务的主要内容,如果未能解决你的问题,请参考以下文章