How do I start a java app in a new background process?

I want to start a java application from my jruby script.
The java application will be running for a long time so I just want to
start it and then continue the jruby job.
When the jruby job is done it will finish, leaving the java app running.

I have a working solution using “pipe = IO.popen(startJavaStr);” but I
don’t need the pipe between the processes.
And I don’t like leaving the pipe open (pipe.close() closes the java
app, right?).

Running jruby 1.3 (ruby 1.8).

So you want to:

  • start a java application from your jruby script
  • you don’t need communication between your script and your java app
  • you want the java app to keep running once the jruby process
    terminates (or do you want the jruby script to finish its job, and
    then wait for the java application to terminate before the jruby
    process terminates)

Is that right?

On Wed, 2011-03-16 at 06:20 -0700, [email protected] wrote:


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Nick G.
Developer @ Media Service Provider
+44 207 729 4797

You could take a look at the spoon gem -
http://www.ruby-forum.com/topic/205568

I’ve not used it, but it looks like it’ll do what you need. What
platform are you running this on?


Nick G.
Developer @ Media Service Provider
+44 207 729 4797

Thanks for the response.

I don’t want the jruby script to care anything about the java job, so:

  • start a java application from your jruby script
    true
  • you don’t need communication between your script and your java app
    true
  • you want the java app to keep running once the jruby process
    terminates
    true

On 3/17/11 11:36 PM, [email protected] wrote:

Thanks for the response.

I don’t want the jruby script to care anything about the java job, so:

  • start a java application from your jruby script
    true
  • you don’t need communication between your script and your java app
    true
  • you want the java app to keep running once the jruby process terminates
    true

This got me wondering, why are you using jruby to start this java app?
Is there a reason a shell script couldn’t be used?

~Rob

It’s for the linux platform.

I will take a look at it, but it just seems like overkill to use an
external package for a task that seems quite trivial?

Thanks for the help!

On Fri, Mar 18, 2011 at 8:57 AM, [email protected]
[email protected]wrote:

It’s for the linux platform.

I will take a look at it, but it just seems like overkill to use an
external package for a task that seems quite trivial?

A problem trivial to describe does not mean it’s trivial to solve.

I want to start the app under certain circumstances, like ‘start if it’s
not already running’. (In the middle of a quite large jruby script).

I guess I could call a bash-script or something from jruby to start the
java app, but I don’t really like that solution.
Is that the solution you where thinking of?

True :slight_smile:

— On Fri, 3/18/11, Michael C. [email protected]
wrote:

From: Michael C. [email protected]
Subject: Re: [jruby-user] How do I start a java app in a new background
process?
To: [email protected]
Date: Friday, March 18, 2011, 6:41 AM

On Fri, Mar 18, 2011 at 8:57 AM, [email protected]
[email protected] wrote:

It’s for the linux platform.

I will take a look at it, but it just seems like overkill to use an
external package for a task that seems quite trivial?

A problem trivial to describe does not mean it’s trivial to solve.

You could cron a script that would check for something (like a file,
etc.) that the Ruby script created, and if it found that it would start
the Java app. This would make it run independently from the Ruby script.

Gary

Bash has “disown” for this. Completely disconnects the child from the
parent.

Can you properly daemonize/background a process purely with bash? I
know you can background it and NOHUP it, but I’m not sure what will
happen when the parent process (the jruby script) exits. My
understanding is that you need to setsid the java app so that it will be
unaffected by whatever happens to the parent process?

On Fri, 2011-03-18 at 06:20 -0700, Rob S. wrote:

To: [email protected]
then wait for the java application to
so I just want to start it and then continue the jruby job.

 http://xircles.codehaus.org/manage_email
 http://xircles.codehaus.org/manage_email

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Nick G.
Developer @ Media Service Provider
+44 207 729 4797

On 3/18/11 7:03 AM, [email protected] wrote:

I want to start the app under certain circumstances, like ‘start if it’s not
already running’. (In the middle of a quite large jruby script).

I guess I could call a bash-script or something from jruby to start the java
app, but I don’t really like that solution.
Is that the solution you where thinking of?

Is there something the jruby script is doing that relies on the java
app ( or vice versa )? From your previous posts it has sounded like
they are independent. If you want you jruby script to check if the
java app running and start it if it isn’t that sounds like there is a
reason for the java app to be running when the jruby script is.

If you just need the java app to be able to be started, stopped, or
restarted you might look into the Apaceh Commons Daemon project
( http://commons.apache.org/daemon/ ). This should allow you check
on the status of the and start it necessary.

~Rob

On 3/21/11 12:40 AM, [email protected] wrote:

I guess I could have been more detailed about what the script does, I just
thougt it would be a no brainer to start and forget the java app.

The jruby script is driving the server part of our application consisting of a
few jruby scripts and some java applications.
One of jruby script tasks is to make sure a java application is running.
The java application is a db integrity checker that’s running in the background
all the time.
Some running options for the java application might be changed from the jruby
script (by changing a properties file).
Today we’re using popen to start the java application and it’s working alright.
Since we don’t need to communicate directly to the java app I just wanted to use
something simpler to start the app.
One more concern is: popen returns a pipe (IO Object?) that is never closed by
the script since the java app is supposed to run longer than the script. So now
we’re trusting jruby (or the os?) to clean up that for us wich I don’t really
like.

Have you tried using Process.fork and Process.detach ? The fork could
execute a block that simple does a Process.exec of the Java app.

Seems worth a try.

~Rob

I tried fork, but got some error like ‘not implemented’ in jruby.
Read that you could pass a flag to jruby at startup to use some
experimental implementation of fork but that didn’t seem like an
interesting solution.

Thanks for all help and patience guys, I’m taking a break from this
jruby issue for a while.

I’m not sure was this fixed in jruby 1.6, but we had similar problem
with 1.5.x versions. The solution was quite rigid, as it relied on
shell forking. OTOH, you can always fallback on java libs :wink:

Best,
Sanel

On Wed, Mar 23, 2011 at 8:02 AM, [email protected]

I guess I could have been more detailed about what the script does, I
just thougt it would be a no brainer to start and forget the java app.

The jruby script is driving the server part of our application
consisting of a few jruby scripts and some java applications.
One of jruby script tasks is to make sure a java application is running.
The java application is a db integrity checker that’s running in the
background all the time.
Some running options for the java application might be changed from the
jruby script (by changing a properties file).
Today we’re using popen to start the java application and it’s working
alright.
Since we don’t need to communicate directly to the java app I just
wanted to use something simpler to start the app.
One more concern is: popen returns a pipe (IO Object?) that is never
closed by the script since the java app is supposed to run longer than
the script. So now we’re trusting jruby (or the os?) to clean up that
for us wich I don’t really like.

On 3/23/11 12:02 AM, [email protected] wrote:

I tried fork, but got some error like ‘not implemented’ in jruby.
Read that you could pass a flag to jruby at startup to use some experimental
implementation of fork but that didn’t seem like an interesting solution.

Sorry, my bad. Process.respond_to(:fork) returning ‘true’ fooled me.

When you get back to looking at this problem check out Charles Nutter’s
blog post here -
http://blog.headius.com/2009/05/fork-and-exec-on-jvm-jruby-to-rescue.html

~Rob

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs