1 2 Previous Next 18 Replies Latest reply on Nov 23, 2006 10:34 PM by masijade

    Running java server on remote machine using SSH

    807607
      I wrote a java server in windows and I need to learn how to run this server (and close it when necessary) on a remote machine in red hat 9 linux... I never used linux before and I never used SSH.. Is there any material I can learn from that discusses this?
        • 1. Re: Running java server on remote machine using SSH
          807607
          There's a bunch. More than you could read in a lifetime. What specifically are you looking for.
          • 2. Re: Running java server on remote machine using SSH
            807607
            how about this.. Is it possible to run the java server on the remote machine even after closing SSH? or do I have to leave SSH open?

            Message was edited by:
            Conqueran
            • 3. Re: Running java server on remote machine using SSH
              807607
              yes its possible to run after closing the window. you have 2 options:

              1)
              java -jar somejar.jar &
              the & at the end starts the process in the background, and you can close your shit and it will run.

              2)
              get a program called screen (GNU Screen)
              this is a bit trickier, esp since you dont know nix, but it can let you open a virtual session within your session, run the command, detach from the session, logout; login reattach and its like you never logged out.

              screen is nice because it saves all your history and does a whole bunch of other useful stuff.

              i usually like to make a shell script for running my server on nix. so you would need to do this:
              make a new file called run.sh
              in that file add all the commands you need to start your server (like a windows batch file)
              after you save the file, at the command line type
              chmod +x run.sh
              (you have to be in the right dir)
              then type
              ./run.sh
              which will start your server.
              • 4. Re: Running java server on remote machine using SSH
                807607
                thank you..

                Another question. How is GNU Screen different from say... a program like Putty?
                • 5. Re: Running java server on remote machine using SSH
                  807607
                  putty is an ssh client

                  screen is something you run on the nix box.
                  • 6. Re: Running java server on remote machine using SSH
                    807607
                    starting the process in the background seems easiest, however if I do that, I won't be able to see the server responses (messages from the server) in the command prompt.. Or Will I? What if I want to see what the server sends out?
                    • 7. Re: Running java server on remote machine using SSH
                      807607
                      For server processes (on any platform) you shouldn't rely on output to the console. Either redirect your System.out / System.err to a file or use a logging package like Log4J.
                      • 8. Re: Running java server on remote machine using SSH
                        jwenting
                        and if you use screen to run it you can just attach to that screen and see the output there :)
                        But most everything should indeed go to file.
                        • 9. Re: Running java server on remote machine using SSH
                          807607
                          writing to a file is a good idea.. one problem I foresee is never knowing when to close the file.. The server runs for an indefinite amount of time, and I plan on using the kill command when i close it.. One thing I can do is simply

                          BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
                          out.write("aString");
                          out.close();

                          open and close the file every time I write a message.. but this seems wasteful.. Another thing I can do is open the file at the beginning, and write to it, but never close it.. But never closing the file might lead to problems? Does anyone know of a good solution?

                          Message was edited by:
                          Conqueran
                          • 10. Re: Running java server on remote machine using SSH
                            807607
                            yes its possible to run after closing the window. you
                            have 2 options:

                            1)
                            java -jar somejar.jar &
                            the & at the end starts the process in the
                            background, and you can close your shit and it will
                            run.
                            is that true? it's been a while since I've had to do this, so please do correct me if I'm wrong, but wouldn't that process still close down when his session closes? I thought 'nohup' would be the answer
                            • 11. Re: Running java server on remote machine using SSH
                              masijade
                              yes its possible to run after closing the window.
                              you
                              have 2 options:

                              1)
                              java -jar somejar.jar &
                              the & at the end starts the process in the
                              background, and you can close your shit and it
                              will
                              run.
                              is that true? it's been a while since I've had to do
                              this, so please do correct me if I'm wrong, but
                              wouldn't that process still close down when his
                              session closes? I thought 'nohup' would be the answer
                              It depends on the shell. zsh and bash automatically handle background processes as if they were nohup-ed, but yes, otherwise you would have to nohup.
                              • 12. Re: Running java server on remote machine using SSH
                                807607
                                It depends on the shell. zsh and bash automatically
                                handle background processes as if they were nohup-ed,
                                but yes, otherwise you would have to nohup.
                                aha. that'll be it. I wasn't using those shells, we were all about korn and c-shell

                                thanks!
                                • 13. Re: Running java server on remote machine using SSH
                                  jwenting
                                  writing to a file is a good idea.. one problem I
                                  foresee is never knowing when to close the file.. The
                                  server runs for an indefinite amount of time, and I
                                  plan on using the kill command when i close it.. One
                                  thing I can do is simply
                                  That's bad. Kill prevents nice shutdown of resources, can lead to data corruption.
                                  open and close the file every time I write a
                                  message.. but this seems wasteful.. Another thing I
                                  can do is open the file at the beginning, and write
                                  to it, but never close it.. But never closing the
                                  file might lead to problems? Does anyone know of a
                                  good solution?
                                  Use a decent logging API. It'll have systems to handle all that automatically (including logfile rotation).
                                  • 14. Re: Running java server on remote machine using SSH
                                    807607
                                    It depends on the shell. zsh and bash automatically
                                    handle background processes as if they were nohup-ed,
                                    but yes, otherwise you would have to nohup.
                                    ok I read about nohup..

                                    I know I can type

                                    nohup java ChatServer 5000 &

                                    and it will run in the background after closing session..
                                    out of curiosity, what would happen if I didnt type the ampersand and I exited? the process would still run on the command line right? Could I log back in and still see it?

                                    Message was edited by:
                                    Conqueran
                                    1 2 Previous Next