将批处理文件作为 Windows 服务运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/415409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run batch file as a Windows service
提问by publicRavi
In order to run one application, a batch file has to be kicked off (which does things like start Jetty, display live logs, etc). The application will work only if this batch file is running. I am hence forced to have this batch file running and not logout from the Windows server.
为了运行一个应用程序,必须启动一个批处理文件(它执行诸如启动 Jetty、显示实时日志等)。仅当此批处理文件正在运行时,应用程序才会工作。因此,我被迫运行此批处理文件,而不是从 Windows 服务器注销。
Can this batch file be run as a service? I am experimenting with one of the suggestions from a similar question.
这个批处理文件可以作为服务运行吗?我正在试验来自类似问题的建议之一。
回答by Junior Mayhé
NSSMis totally free and hyper-easy, running command prompt / terminal as administrator:
NSSM是完全免费且超级简单的,以管理员身份运行命令提示符/终端:
nssm install "YourCoolServiceNameLabel"
then a dialog will appear so you can choose where is the file you want to run.
然后将出现一个对话框,以便您可以选择要运行的文件的位置。
to uninstall
卸载
nssm remove "YourCoolServiceNameLabel"
回答by Stephen Martin
Why not simply set it up as a Scheduled Task that is scheduled to run at start up?
为什么不简单地将其设置为计划在启动时运行的计划任务?
回答by w00ngy
There's a built in windows cmd to do this: sc create. Not as fancy as nssm, but you don't have to download an additional piece of software.
有一个内置的 Windows cmd 可以执行此操作:sc create。不像 nssm 那样花哨,但您不必下载额外的软件。
sc create "ServiceName" start= demand displayname= "DisplayName" binpath= [path to .bat file]
Note
笔记
- start=demand means you must start the service yourself
- whitespace is required after
=
- I did encounter an error on service start that the service did not respond in a timely manner, but it was clear the service had run the .bat successfully. Haven't dug into this yet but this threadexperienced the same thing and solved it using nssm to install the service.
- start=demand 表示您必须自己启动服务
- 之后需要空格
=
- 我确实在服务启动时遇到错误,服务没有及时响应,但很明显该服务已成功运行 .bat。还没有深入研究这个问题,但是这个线程遇到了同样的事情,并使用 nssm 安装服务解决了这个问题。
回答by Zediiiii
No need for extra software. Use the task scheduler -> create task -> hidden. The checkbox for hidden is in the bottom left corner. Set the task to trigger on login (or whatever condition you like) and choose the task in the actions tab. Running it hidden ensures that the task runs silently in the background like a service.
不需要额外的软件。使用任务调度程序 -> 创建任务 -> 隐藏。隐藏的复选框位于左下角。将任务设置为在登录时触发(或您喜欢的任何条件)并在操作选项卡中选择任务。隐藏运行可确保任务像服务一样在后台静默运行。
Note that you must also set the program to run "whether the user is logged in or not" or the program will still run in the foreground.
请注意,您还必须将程序设置为运行“无论用户是否登录”,否则程序仍将在前台运行。
回答by lexu
As Doug Currie says use RunAsService.
正如 Doug Currie 所说,使用 RunAsService。
From my past experience you must remember that the Service you generate will
根据我过去的经验,您必须记住,您生成的服务将
- have a completely different set of environment variables
- have to be carefully inspected for rights/permissions issues
- might cause havoc if it opens dialogs asking for any kind of input
- 有一组完全不同的环境变量
- 必须仔细检查权利/权限问题
- 如果它打开要求任何类型的输入的对话框,可能会造成严重破坏
not sure if the last one still applies ... it was one big night mare in a project I worked on some time ago.
不确定最后一个是否仍然适用......这是我前段时间参与的一个项目中的一个大噩梦。
回答by Nicholas
While it is not free (but $39), FireDaemonhas worked so well for me I have to recommend it. It will run your batch file but has loads of additional and very useful functionality such as scheduling, service up monitoring, GUI or XML based install of services, dependencies, environmental variables and log management.
虽然它不是免费的(但 39 美元),但FireDaemon对我来说效果很好,我不得不推荐它。它将运行您的批处理文件,但具有大量额外且非常有用的功能,例如调度、服务监控、基于 GUI 或 XML 的服务安装、依赖项、环境变量和日志管理。
I started out using FireDaemon to launch JBoss application servers (run.bat) but shortly after realized that the richness of the FireDaemon configuration abilities allowed me to ditch the batch file and recreate the intent of its commands in the FireDaemon service definition.
我开始使用 FireDaemon 来启动 JBoss 应用程序服务器 ( run.bat),但不久之后我意识到 FireDaemon 配置功能的丰富性让我可以放弃批处理文件并在 FireDaemon 服务定义中重新创建其命令的意图。
There's also a SUPER FireDaemon called Trinity which you might want to look at if you have a large number of Windows servers on which to manage this service (or technically, any service).
还有一个名为 Trinity 的 SUPER FireDaemon,如果您有大量 Windows 服务器来管理此服务(或从技术上讲,任何服务),您可能需要查看它。
回答by CoreTech
回答by Goofyseeker311
On Windows 2019 Server, you can run a Minecraft java server with these commands:
在 Windows 2019 Server 上,您可以使用以下命令运行 Minecraft java 服务器:
sc create minecraft-server DisplayName= "minecraft-server" binpath= "cmd.exe /C C:\Users\Administrator\Desktop\rungui1151.lnk" type= own start= auto
sc create minecraft-server DisplayName= "minecraft-server" binpath="cmd.exe /CC:\Users\Administrator\Desktop\rungui1151.lnk" type= own start= auto
The .lnk file is a standard windows shortcut to a batch file.
.lnk 文件是批处理文件的标准 Windows 快捷方式。
--- .bat file begins ---
--- .bat 文件开始 ---
java -Xmx40960M -Xms40960M -d64 -jar minecraft_server.1.15.1.jar
java -Xmx40960M -Xms40960M -d64 -jar minecraft_server.1.15.1.jar
--- .bat file ends ---
--- .bat 文件结束 ---
All this because:
这一切都是因为:
service does not know how to start in a folder,
服务不知道如何在文件夹中启动,
cmd.exe does not know how to start in a folder
cmd.exe 不知道如何在文件夹中启动
Starting the service will produce "timely manner" error, but the log file reveals the server is running.
启动服务会产生“及时方式”错误,但日志文件显示服务器正在运行。
If you need to shut down the server, just go into task manager and find the server java in background processes and end it, or terminate the server from in the game using the /stop command, or for other programs/servers, use the methods relevant to the server.
如果您需要关闭服务器,只需进入任务管理器在后台进程中找到服务器java并结束它,或者使用/stop命令从游戏中终止服务器,或者对于其他程序/服务器,使用方法与服务器有关。