如何从命令行停止OSGI应用程序 [英] How to stop an OSGI Application from command line

查看:521
本文介绍了如何从命令行停止OSGI应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行的osgi(equinox容器)应用程序。它将通过bash脚本启动。 查看felix gogo shell

I do have a running osgi (equinox container) application. It will been started via a bash script. See felix gogo shell

java -jar ... _osgi.jar -console 1234 &

这一切都很好,我也可以通过

This all works pretty well and I also can stop it via

telnet localhost 1234
<osgi>stop 0

但我正在寻找的是如何将其嵌入到bash脚本中以停止osgi应用程序。

But what I am looking for is how can I embed this into a bash script to stop the osgi application.

我已经尝试过这个

echo stop 0 | telnet localhost 1234

但这不起作用。所以如果有人知道如何把它放在一个bash脚本中,请告诉我。

but this doesn't work. So if someone has idea how to put this in a bash script, please let me know.

推荐答案

Telnet到Gogo shell似乎像一个非常脆弱的解决方案。为什么不编写应用程序来支持标准POSIX信号处理?然后你可以用 kill -s TERM< pid> 来杀死它。

Telneting into the Gogo shell seems like an awfully fragile solution. Why not write your application to support standard POSIX signal handling? Then you could simply kill it with kill -s TERM <pid>.

例如以下的捆绑激活器安装一个干净关闭框架的关闭钩子,等效于 stop 0

For example the following bundle activator installs a shutdown hook that cleanly shuts down the framework, equivalently to stop 0:

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.launch.Framework;

public class ShutdownHookActivator implements BundleActivator {

    @Override
    public void start(final BundleContext context) {
        Thread hook = new Thread() {
            @Override
            public void run() {
                System.out.println("Shutdown hook invoked, stopping OSGi Framework.");
                try {
                    Framework systemBundle = context.getBundle(0).adapt(Framework.class);
                    systemBundle.stop();
                    System.out.println("Waiting up to 2s for OSGi shutdown to complete...");
                    systemBundle.waitForStop(2000);
                } catch (Exception e) {
                    System.err.println("Failed to cleanly shutdown OSGi Framework: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        };
        System.out.println("Installing shutdown hook.");
        Runtime.getRuntime().addShutdownHook(hook);
    }

    @Override
    public void stop(BundleContext context) throws Exception {
    }

}

注意:如果您控制启动OSGi框架的启动程序代码,您应该在那里安装关闭钩子而不是单独捆绑。

NB: if you are in control of the launcher code that starts the OSGi Framework, you should probably install the shutdown hook there rather from a separate bundle.

在bash中, $!变量计算结果为上次执行的后台命令的PID。您可以将其保存到您自己的变量中以供日后参考,例如:

In bash, the $! variable evaluates to the PID of the last executed background command. You can save this into your own variable for later reference, e.g.:

# Launch app:
java -jar ... &
MY_PID=$!

# Later when you want to stop your app:
kill $MY_PID

这篇关于如何从命令行停止OSGI应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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