终止在Java下的另一个用户帐户下运行的进程 [英] Kill a process running under another user account in Java

查看:58
本文介绍了终止在Java下的另一个用户帐户下运行的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



当我启动一个程序时(我们称之为OnlyYou.jar) )我需要杀死它的所有其他运行实例,无论是谁启动它们 - 我自己或任何其他用户。

(原因:此过程需要由另一个应用程序和监视器监视不适用于多个实例。)



但似乎Java不允许用户能够看到在其他帐户下运行的进程。对吗?



我有一种体验,让外国实例的PID运行tasklist命令并将其传递给应用程序,但是(如预期的那样)这个没有任何影响。

此PID不会被我的用户识别。



有什么建议吗?或者这个问题是不可能用Java解决的?



谢谢!

Hi there!

When I launch a process (let´s call it "OnlyYou.jar") I need to kill all the other running instances of it, no matter who launched them - myself or any other user.
(The reason: this process needs to be monitored by another app and the monitor doesn´t work well with multiple instances.)

But it seems that Java doesn´t allow that a user be able to see processes running under other accounts. Right ?

I did an experience getting the PID of the foreign instance running the "tasklist" command and passing it to the app, but (as expected) this hasn´t any effect.
This PID keeps not being recognized under my user.

Any suggestions ? Or is this question impossible to be solved in Java ?

Thanks !

推荐答案

杀死一个流程另一个用户需要以root / administrator身份执行kill命令,这通常不是一个选项。



但你可以在你的实现某种IPC(进程间通信)响应特定命令以终止进程的应用程序。一个例子是使用在本地端口上侦听的TCP或UDP服务器。



另一个常见的解决方案是反其道而行:

拒绝启动应用程序,如果另一个实例已在运行。

这可以通过使用某种互斥锁或锁定文件来检测。



锁定文件也可以与IPC方法一起使用来检查是否需要终止另一个进程。



伪代码:

Killing a process started by another user requires executing the kill command as root/administrator which is usually not an option.

But you may implement some kind of IPC (Inter Process Communication) in your application that reacts on a specific command to terminate the process. An example would be using a TCP or UDP server listening on a localhost port.

Another common solution is doing the opposite:
Deny starting the application if another instance is already running.
This can be detected by using some kind of mutex or a lock file.

A lock file can be also used with the IPC method to check if another process needs to be terminated.

Pseudo code:
if lock file exists
    send terminate command via socket
    wait until lock file has been deleted
create lock file
start server to listen for termination command

server:
if termination command received
    cleanup (stop server)
    delete lock file
    terminate


这篇关于终止在Java下的另一个用户帐户下运行的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆