即使在用户注销后也在 Windows 中运行 Java 进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/553175/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:59:27  来源:igfitidea点击:

Running a Java process in Windows even after the user is logged out

javawindowswindows-services

提问by cdoe

I have a batch file that starts a Java process in a Windows 2003 server. As per the security policy, the users of that machine are logged off forcefully, if the user is inactive for a certain period of time. The problem is that when the user is logged out, the process also dies.

我有一个在 Windows 2003 服务器中启动 Java 进程的批处理文件。根据安全策略,如果用户在一段时间内处于非活动状态,则该计算机的用户将被强制注销。问题是当用户注销时,进程也会终止。

I scheduled a new task (Control Panel -> Scheduled Tasks) and selected the option of 'When my computer starts' and gave the user account details there. But it doesn't seem to have any effect, the user is still logged out and the process dies. Is a reboot necessary to make this change effective? And after the reboot, will I achieve what I'm expecting (keeping the process alive)?

我安排了一个新任务(控制面板 -> 计划任务)并选择了“我的计算机启动时”选项并在那里提供了用户帐户详细信息。但它似乎没有任何效果,用户仍然注销并且进程终止。是否需要重新启动才能使此更改生效?重新启动后,我会实现我的期望(保持进程活跃)吗?

Alternatively, will running this process as a Windows Service solve the problem? If so, can you please let me know how I can make a Java program or a batch file to run as a Windows Service? I would prefer not to use any other third party tools or libraries.

或者,将此进程作为 Windows 服务运行会解决问题吗?如果是这样,您能否让我知道如何制作 Java 程序或批处理文件以作为 Windows 服务运行?我不想使用任何其他第三方工具或库。

Thanks

谢谢

回答by Nick Fortescue

If you want it to run under Scheduled tasks you have to make sure you don't have "only run when user logged in" checked, which usually means you need to supply a password.

如果您希望它在计划任务下运行,您必须确保没有选中“仅在用户登录时运行”,这通常意味着您需要提供密码。

A windows service would be the normal way to do this: the Java service wrapperis 3rd party but loads of people use it.

Windows 服务将是执行此操作的正常方法:Java 服务包装器是 3rd 方,但很多人都在使用它。

If you really wanted to not use a 3rd party method you could use svrany.exe (http://support.microsoft.com/kb/137890)on WIndows NT or later, but it is not designed specifically for Java.

如果您真的不想使用第 3 方方法,您可以在WINdowsNT 或更高版本上使用svrany.exe (http://support.microsoft.com/kb/137890),但它不是专门为 Java 设计的。

回答by Jesse Weigert

Wrapping the process with srvany.exe and launching as a service would work as well.

用 srvany.exe 包装进程并作为服务启动也可以。

http://support.microsoft.com/kb/137890

http://support.microsoft.com/kb/137890

回答by cdoe

I'm using Java Service Wrapperto start the java process as windows service. I guess it works similary to the srvany.exe mentioned in the previous posting.

我正在使用Java Service Wrapper将 java 进程作为 windows 服务启动。我想它的工作原理类似于上一篇文章中提到的 srvany.exe。

回答by Konstantin

I don't know it this is relevant but we are using a flag to the jvm so it does not shutdown on logoffs

我不知道这是相关的,但我们对 jvm 使用了一个标志,因此它不会在注销时关闭

"java -Xrs"

Link to suns description of -Xrs

链接到太阳对 -Xrs 的描述

回答by Arun Pratap

As per my analysis,

根据我的分析,

The Idle Solution will be writing a VC++ (.net) Windows Service creation program to launch the .bat / .exe as a System service with all the required call back methods to SCM.

空闲解决方案将编写一个 VC++ (.net) Windows 服务创建程序,以将 .bat / .exe 作为系统服务启动,并提供对 SCM 的所有必需回调方法。

Note: 1. Wrapping the process with srvany.exe would not work as it does not have any call back process to the SCM (Service Control Manager). 2. And java service Wrapper is a third party API (many are LGPL licensed).

注意: 1. 用 srvany.exe 包装进程是行不通的,因为它没有对 SCM(服务控制管理器)的任何回调进程。2.和java服务Wrapper是第三方API(很多是LGPL许可的)。