1

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.

4

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
  • Thanks this is great! :) I think screen is the way to go. – user3772108 Feb 24 '16 at 16:12
  • How to use screen to show output from a process ID? – panoet Dec 2 '19 at 2:52
3

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.

  • I am so dump. Thanks this solves the issue. But one more question: Is it able to fg it for every connected user? – user3772108 Feb 24 '16 at 16:07
  • @user3772108 If one user started and backgrounded it, another user cannot bring it to the foreground. Use screen or tmux if that's the need. – muru Feb 24 '16 at 16:09
  • I understand. I will try to get some knowledge about screen. Thanks for this great help! :) – user3772108 Feb 24 '16 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.