How can I run a Java program in background?
Solution 1
You can't foreground a process from a different sesion. The functionality you want is probably possible with the screen
command.
As for the java app failing to run in init.d; you are not changing to the /opt/glassfish/domains/domain1/applications/AS/WEB-INF/classes
folder. You have '.' in your classpath so it matters where you run from.
Ideally, I think you should keep the bash script and run that from init.d. However, you could also add the full path to your init.d classpath explicitly. Though I suspect you will need to use screen
because of the inability to foreground the app.
For screen, this is an example of what you are looking for
$ screen -S myScreen -d -m ./dostuff.sh
$ screen -r myScreen
Solution 2
fg
uses job numbers, not PIDs:
fg: fg [job_spec]
Move job to the foreground.
The job number is the number in brackets ([1]
in this case).
Instead of fg 10119
, use either of:
fg
fg 1
If it is to be run in an init script, you should use screen
(or tmux
) if you want arbitrary users to bring it to the foreground.
Related videos on Youtube
user3772108
Updated on September 18, 2022Comments
-
user3772108 over 1 year
I try to make a
init.d
script which should start this java command:java -cp ./:/opt/glassfish/domains/domain1/lib/*:/opt/glassfish/lib/* com.name.it.svcimpl.OrderRequestDispatcher &
With
&
it starts as background process and this is fine. The problems are starting when you are trying to get it to the foreground:[1] 10119 user@server:$ fg 10119 -bash: fg: 10119: no such job
After you are pressing enter this happens:
[1]+ Stopped java -cp ./:/opt/glassfish/domains/domain1/lib/*:/opt/glassfish/lib/* name.alcar.it.svcimpl.OrderRequestDispatcher
As you see there was no process like
10119
so why does it stop? But the things getting much more difficult when the command is in a init.d bash script:#!/bin/bash (cd /opt/glassfish/domains/domain1/applications/AS/WEB-INF/classes; java -cp ./:/opt/glassfish/domains/domain1/lib/*:/opt/glassfish/lib/* com.name.it.svcimpl.OrderRequestDispatcher &)
(Need to started under the path
/opt/glassfish/domains/domain1/applications/AS/WEB-INF/classes
)Now my question as not java programmer is:
- Is it possible to run a java program in background with the option to foreground it from any terminal session?
As you can read it is really important to run it in background. Any user that are logged in should be able to put it to the foreground. The users are connecting with SSH to the terminal.
-
user3772108 about 8 yearsI am so dump. Thanks this solves the issue. But one more question: Is it able to fg it for every connected user?
-
muru about 8 years@user3772108 If one user started and backgrounded it, another user cannot bring it to the foreground. Use
screen
ortmux
if that's the need. -
user3772108 about 8 yearsThanks this is great! :) I think
screen
is the way to go. -
user3772108 about 8 yearsI understand. I will try to get some knowledge about
screen
. Thanks for this great help! :) -
panoet over 4 yearsHow to use screen to show output from a process ID?