“注射”命令进入正在运行的进程
我制作了一个 Perl 脚本来启动 Java 游戏服务器, java -jar somejar.jar > /dev/null 2>&1 &
它正常启动并运行,但我希望能够将命令“注入”到正在运行的服务器控制台(以停止重新启动等),因为停止是危险的通过killall 或^C 终止它。
正常运行时,服务器会显示用户活动日志和一个用于键入命令的区域,这就是我想要“注入”文本的地方。
这可能吗?
谢谢! 贾斯汀
I've made a Perl script to start a Java game server, java -jar somejar.jar > /dev/null 2>&1 &
It starts and runs normally, but I would like to be able to 'inject' commands into the running server console (to stop restart etc) because it is dangerous to stop by terminating it via killall or ^C.
When run normally the server displays a log of user activities and an area to type commands in, that is where I would like to 'inject' the text.
Is this possible?
Thanks!
Justin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不调整 Java 应用程序就这样做是不合理的。更改您的应用程序,使其既可以从输入控制台读取命令,也可以从套接字读取命令。创建一个服务器套接字来侦听传入连接,读取数据并将其解释为命令。从 Perl 脚本或任何要执行命令的地方,连接到服务器套接字的侦听端口并将命令字符串写入套接字。
您甚至可以创建一个与“命令源”无关的干净抽象。
请注意,有一个受限的允许您捕获和解释 POSIX 信号的 API,但它是隐藏的,可能不应该使用。
It won't be reasonable to do this without tweaking the Java application. Change your application such that it can read commands from both the input console but also from a socket. Create a server socket listening for incoming connections, read the data and interpret it as a command. From the Perl script or wherever you want to execute the command, connect to the server socket's listening port and write the command string to the socket.
You can even create a clean abstraction that will be agnostic to the "command source".
Note that there is a restricted API that allows you to trap and interpret POSIX signals but it's hidden and should probably not be used.
正如 这个问题中所讨论的,将 stdin 绑定到 FIFO 将不起作用,因为您的每次将新命令传递到 FIFO 后,服务器进程都会看到一个单独的 EOF。我猜最简单的解决方案是在 屏幕 中运行服务器。更好但更具技术性的解决方案是修改服务器以通过套接字等接受命令。
As discussed in this question, binding stdin to a FIFO won't work, because your server process would see a separate EOF after each time you pass new commands into the FIFO. I'm guessing the simplest solution would be to run the server in Screen. The nicer, but more technical solution would be to modify the server to accept commands through for example a socket.