将批处理文件作为Windows服务运行

时间:2022-12-20 02:17:19

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.

此批处理文件可以作为服务运行吗?我正在尝试一个类似问题的建议之一。

7 个解决方案

#1


NSSM is 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"

#2


Why not simply set it up as a Scheduled Task that is scheduled to run at start up?

为什么不简单地将其设置为计划在启动时运行的计划任务?

#3


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.

不确定最后一个是否仍然适用...这是我前一段时间工作过的一个大夜晚的母马。

#4


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
  • start = demand意味着您必须自己启动服务

  • 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 thread experienced the same thing and solved it using nssm to install the service.
  • 我确实在服务启动时遇到了服务没有及时响应的错误,但很明显服务已成功运行.bat。还没有挖到这个但是这个线程经历了同样的事情并使用nssm来解决它来安装服务。

#5


While it is not free (but $39), FireDaemon has 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服务器来管理这项服务(或者从技术上讲,任何服务),你可能会想看看它。

#6


AlwaysUp will easily run your batch file as a service. It is similar to FireDaemon (mentioned above) and isn't free, but you may find the rich feature set to be an asset in a professional environment.

AlwaysUp可以轻松地将批处理文件作为服务运行。它类似于FireDaemon(如上所述)并且不是免费的,但您可能会发现丰富的功能集在专业环境中是一种资产。

Good luck!

#7


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.

请注意,您还必须将程序设置为“无论用户是否登录”,或者程序仍将在前台运行。

#1


NSSM is 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"

#2


Why not simply set it up as a Scheduled Task that is scheduled to run at start up?

为什么不简单地将其设置为计划在启动时运行的计划任务?

#3


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.

不确定最后一个是否仍然适用...这是我前一段时间工作过的一个大夜晚的母马。

#4


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
  • start = demand意味着您必须自己启动服务

  • 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 thread experienced the same thing and solved it using nssm to install the service.
  • 我确实在服务启动时遇到了服务没有及时响应的错误,但很明显服务已成功运行.bat。还没有挖到这个但是这个线程经历了同样的事情并使用nssm来解决它来安装服务。

#5


While it is not free (but $39), FireDaemon has 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服务器来管理这项服务(或者从技术上讲,任何服务),你可能会想看看它。

#6


AlwaysUp will easily run your batch file as a service. It is similar to FireDaemon (mentioned above) and isn't free, but you may find the rich feature set to be an asset in a professional environment.

AlwaysUp可以轻松地将批处理文件作为服务运行。它类似于FireDaemon(如上所述)并且不是免费的,但您可能会发现丰富的功能集在专业环境中是一种资产。

Good luck!

#7


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.

请注意,您还必须将程序设置为“无论用户是否登录”,或者程序仍将在前台运行。