简体   繁体   English

如何防止Nohup杀死等待stdin linux的java进程

[英]how to keep nohup from killing java process that waits for stdin linux

I have a java program that is executed on command line (Bash Linux). 我有一个在命令行(Bash Linux)上执行的Java程序。 Once executed the program continues to run, reading stdin until the user inputs 'quit'. 一旦执行,程序将继续运行,读取标准输入,直到用户输入“退出”为止。

Sometimes I would like to nohup the program so it continues to run even if my remote connection is closed. 有时,我想nohup该程序,以便即使关闭远程连接也可以继续运行。 The problem is that when I attempt to nohup the program as demonstrated below it seems that its prompt for stdin kills nohup and terminates the program. 问题是,当我尝试按以下所示对程序进行nohup时,似乎其输入stdin的提示会杀死nohup并终止该程序。

Example: $ nohup java -jar myApp.jar 示例:$ nohup java -jar myApp.jar

I have tried redirecting the program's output to 2>/dev/null per other suggestions as well as running it in the background using & . 我尝试将程序的输出重定向到其他建议的2> / dev / null以及使用在后台运行。 Neither prevents nohup from being killed. 两者都不能阻止nohup被杀死。

I cannot use disown because once I run the program I lose the command prompt to the program. 我不能使用disown,因为一旦运行该程序,我将失去命令提示符。

To get your program running in the background, run tmux : 要使您的程序在后台运行,请运行tmux

tmux

This will open up a tmux shell, where you can run your program: 这将打开一个tmux shell,您可以在其中运行程序:

java -jar myApp.jar

Then to detach from the tmux session, type ctrl-b then d . 然后,要与tmux会话分离,请键入ctrl-b然后输入d Your program will continue to run in the background. 您的程序将继续在后台运行。

To reattach to your program's shell at a later time (to kill it or view its output), run tmux attach . 要在以后重新连接到程序的外壳程序(杀死它或查看其输出),请运行tmux attach

Although tmux solved my problem, I kept poking around and playing with disown until I got it to work. 尽管tmux解决了我的问题,但我一直四处闲逛并玩弄disown,直到我开始使用它。 I was able to keep the java process running after logoff by piping it a super-long sleep command (suggested in this thread ), running it in the background, and then running disown . 我可以通过注销一个超长睡眠命令(在此线程中建议),在后台运行它,然后运行disown ,使注销后的Java进程保持运行状态 Like this: 像这样:

$ sleep 999999999 | $ sleep 999999999 | java -jar myApp.jar& java -jar myApp.jar&

$ disown $舍弃

So, I'm going to stick with tmux , but I just thought I'd share this as it worked as well for my problem. 因此,我将坚持使用tmux ,但我只是想分享一下,因为它可以很好地解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM