发送命令Java的罐子通过/ proc / {} PID / FD / 0使用标准输入 [英] Sending command to java -jar using stdin via /proc/{pid}/fd/0

查看:259
本文介绍了发送命令Java的罐子通过/ proc / {} PID / FD / 0使用标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个命令来使用服务器的Minecraft罐子的/ proc / {} PID / FD / 0,但服务器不执行命令。

I'm trying to send a command to a minecraft server jar using /proc/{pid}/fd/0 but the server does not execute the command.

要复制什么,我试图做的,你能做到这一点基于Debian的机器(可能还有其他的Linux distributuions藏汉)上。

To replicate what I'm trying to do you can do this on a Debian based machine (possibly other Linux distributuions aswell).

我用它来测试这一点:


  • 的Ubuntu 14.04

  • minecraft_server.jar (测试1.8)

  • 的OpenJDK运行时环境(使用默认的JRE - 无头安装)

  • Ubuntu 14.04
  • minecraft_server.jar (testing with 1.8)
  • OpenJDK Runtime Environment (installed with default-jre-headless)

第一个控制台:

First console:

$ java -jar minecraft_server.jar nogui

响应:[...服务器启动并等待输入]

Response: [ ... server starts and waiting for input]

say hi

响应:19时52分23秒] [Server线程/ INFO]:[服务器]喜

Response: [19:52:23] [Server thread/INFO]: [Server] hi

第二个控制台:

Second console:

现在,当我切换到第二个控制台,与服务器仍然在运行的第一个我写:

Now when i switch to the second console, with the server still running in the first i write:

echo "say hi2" >> /proc/$(pidof java)/fd/0

一切看起来都很好,直到我切换回第一个控制台。我可以看到文字说HI2,但服务器不承认它。我可以再次写在第一个控制台另一个命令,这是因为如果从第二个控制台输入文本甚至没有存在过。

Everything looks well until I switch back to the first console. I can see the text "say hi2" but the server hasn't recognized it. I can write another command in the first console again and it is as if the text inputted from the second console hasn't even existed.

这是为什么?而更重要的是,我怎么使用的/ proc / {} PID在将命令发送到一个java jar文件有道/ FD / 0?

Why is this? And more importantly, how do I use /proc/{pid}/fd/0 in a proper way to send commands to a java jar file?

我不知道这是否是某种Java的事情,我不知道,如果在执行服务器的时候,我可以用一些标志或什么的,或者如果它是服务器本身罐子是问题。

I don't know if this is some kind of Java-thing that I'm not aware of, if I can use some flag or something when executing the server, or if it's the server jar itself that is the problem..

我知道,你可以使用屏幕,尾-f或某种服务器包装要做到这一点,但是这不是我后。我想用这种方法来发送一个命令,以某种方式。

I'm aware that you can use screen, tail -f or some kind of server wrapper to accomplish this, but that's not what I'm after. I would like to send a command using this method, in some kind of way.

感谢您的时间。如果我需要澄清的东西,请询问。

Thank you for your time. If I need to clarify something, please ask.

推荐答案

这不是一个Java的事情。你正在尝试根本就不是可行的。

It's not a Java thing. What you are trying is simply not doable.

测试这样的:

控制台1:

 $ cat

这将基本呼应任何你就可以马上输入你打回。

This will basically echo anything you type on it as soon as you hit "return".

Console2:寻找你的命令的进程号。比方说,这是NNN。这样做:

Console2: Find the process number of your cat command. Let's say it's NNN. Do:

$ echo Something > /proc/NNN/fd/0

切换回控制台1。你会看到在控制台输出东西,但它不是回荡。

Switch back to Console1. You'll see "Something" on the console output, but it's not echoed.

为什么呢?做

$ ls -l /proc/NNN/fd

和你们了解。所有这三个描述,0 标准输入 1 标准输出和2 标准错误实际上是符号链接,并且都指向同一个从属伪终端(PTS)设备,这是您的第一个终端关联的点。

And you may understand. All three descriptors, 0 for stdin, 1 for stdout and 2 for stderr are actually symbolic links, and all point to the same pseudoterminal slave (pts) device, which is the pts associated with your first terminal.

因此​​,基本上,当你写,你实际上写入控制台的输出,而不是它的输入。如果从文件中读取,你可以偷一些本来要到这个过程中的第一个控制台(你都在竞相此输入)输入的。这是一个字符设备的工作原理。

So basically, when you write to it, you actually write to the console output, not to its input. If you read from that file, you could steal some of the input that was supposed to go to the process in the first console (you are racing for this input). That's how a character device works.

有关的/ proc文档说:

The documentation for /proc says that:

的/ proc / [PID] / FD /

这是一个包含每个文件一个条目子目录
  该过程由它的文件描述符,命名并
  这是一个符号链接到实际的文件。因此,0是
  标准输入,1个标准输出,2标准错误,等等
  上。

This is a subdirectory containing one entry for each file which the process has open, named by its file descriptor, and which is a symbolic link to the actual file. Thus, 0 is standard input, 1 standard output, 2 standard error, and so on.

因此​​,这些都是不被进程打开实际文件描述符。它们只是链接文件(或在此情况下,字符设备)与指示它们连接到在给定的方法,该方法描述符的名称。他们的主要职责是告诉你该进程是否已经重定向的文件描述符或已经打开任何新的,而且它们指向的资源。

So these are not the actual file descriptors opened by the process. They are just links to files (or in this case, character devices) with names that indicate which descriptor they are attached to in the given process. Their main duty is to tell you whether the process has redirected its file descriptors or has opened any new ones, and which resources they point to.

但如果你想这样做的另一种方式,你可以使用 FIFO - 命名管道

But if you want an alternative way of doing this, you can use a fifo - a named pipe.

做创建一个FIFO:

$ mkfifo myfifo

运行Java程序:

Run your java program:

$ java -jar minecraft_server.jar nogui < myfifo

打开另一个控制台。写

Open another console. write

$ cat > myfifo

现在开始输入的东西。切换到第一个控制台。你会看到你的服务器执行你的命令。

Now start typing things. Switch to the first console. You'll see your server executing your commands.

注意你的结束文件,虽然。几个进程可以写入到同一个 FIFO ,但只要最后一个关闭它,你的服务器将接收的标准输入的EOF。

Mind your end-of-files, though. Several processes can write to the same fifo, but as soon as the last one closes it, your server will receive an EOF on its standard input.

这篇关于发送命令Java的罐子通过/ proc / {} PID / FD / 0使用标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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