How to stop the OSGI application from the command line

I have an osgi application (equinox container). It will be launched through a bash script. See felix gogo shell

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

It all works very well, and I can also stop it through

 telnet localhost 1234 <osgi>stop 0 

But what I'm looking for is how I can embed it in a bash script to stop the osgi application.

I already tried this

 echo stop 0 | telnet localhost 1234 

but it does not work. Therefore, if anyone has an idea how to put this in a bash script, let me know.

+5
source share
2 answers

Telneting into a Gogo shell seems like a very fragile solution. Why not write an application to support standard POSIX signal processing? Then you can just kill it with kill -s TERM <pid> .

For example, the following beam activator sets a disconnect hook that completely closes the framework, equivalent 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 { } } 

NB: if you control the startup code that runs the OSGi Framework, you should probably set the shutdown tap, and not from a separate package.

Update

In bash, the variable $! evaluated as the PID of the last executed background command. You can save this in your own variable for later reference, for example:

 # Launch app: java -jar ... & MY_PID=$! # Later when you want to stop your app: kill $MY_PID 
+9
source

You can, for example, use expect if there is no other way than telnet to close the application.

http://www.tamas.io/automatic-scp-using-expect-and-bash/

0
source

All Articles