我正在远程机器上启动一个java程序,使用ssh登录到机器然后运行命令。 使用JCraft java库创建ssh会话。 该过程正在启动REST服务器,并以“&”放入后台。 该计划如下:
java -cp $ATFIMAGE/atf.jar com.atf.agent.jobs.SlaveRunner dub-001948-VM01 8888 8889 > /home/john/testHome/SlaveRunner_stdout.log 2> /home/john/testHome/SlaveRunner_stderr.log &我目前使用的方法是从远程服务器scp stderr文件并检查它是否为空。 如果文件为空,那么我可以说它正确启动。 这有效,但它并不强大:
session.command(javaCmd); manager.copyArtefactFromTarget(remoteHost, remoteStdErrFile, localFileSystem);如果启动java进程有任何延迟,stderr可以为空,然后在我从服务器收集它之后填充。
使用unix退出代码不是一个选项,因为该过程已放入后台。 您只在进程退出时获取代码,但在这种情况下,进程甚至都没有启动。
只是想知道是否有监控过程,以及它是否正确启动。
I am starting a java program on a remote machine using ssh to log into the machine and then running the command. The ssh session is created using the JCraft java library. The process is starting a REST server, and is put into the background with '&'. The program is started as follows:
java -cp $ATFIMAGE/atf.jar com.atf.agent.jobs.SlaveRunner dub-001948-VM01 8888 8889 > /home/john/testHome/SlaveRunner_stdout.log 2> /home/john/testHome/SlaveRunner_stderr.log &The approach I am using at the moment is to scp the stderr file from the remote server and check if it's empty. If the file is empty, then I can say that it started correctly. This works but it's not that robust:
session.command(javaCmd); manager.copyArtefactFromTarget(remoteHost, remoteStdErrFile, localFileSystem);If there is any delay starting the java process, the stderr can be empty, and then populated after I collect it from the server.
Using the unix exit code is not an option, as the process is put into the background. You only get the code when the process exits, but in this case the process didn't even start.
Just wondering if there is anyway to monitor the process, and whether it started correctly.
最满意答案
您可以通过获取后台任务的进程ID
echo $!
(请参阅如何获取后台进程的PID? )
然后,您可以使用该pid轮询进程是否仍处于活动状态(请参阅https://stackoverflow.com/a/14880926/1015327 )。 如果这个过程死了你可以做出相应的反应 一旦REST服务正常运行,您就可以停止轮询。 如果在某个时间段内无法激活REST服务,您也可以终止该进程。
但是,对我而言,您的协议似乎有点复杂且容易出错。 您是否考虑过运行(Java-)服务器的其他方法?
You can get the process id of your background task via
echo $!
(see How to get PID of background process?)
Then you can use that pid to poll if the process is still alive (see e.g. https://stackoverflow.com/a/14880926/1015327). If the process died you can react accordingly; and you can stop polling once the REST service becomes functional. You can also kill the process if it fails to activate the REST service within a certain timespan.
However, to me it seems that your protocol may be a little complicated and error-prone. Have you considered other approaches for running a (Java-)server?
更多推荐
发布评论