1

I use putty for taking linux machine terminal from my windows machine and run a java program.

Java Class

   try {

        Timer t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                logger.info(new Date()+"...");
                i=i+1;
            }
        }, new Date(), 3000);

        if(i==50){
            t.cancel();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

This program write a line in every 3 second. If I close the putty it stop running. Program become end, it not writing any more lines in the logger file.

How can I run java program in the backend or even in running state after closing putty terminal ?

Kushal Jain
  • 3,029
  • 5
  • 31
  • 48

2 Answers2

8

Try nohup command:

nohup java -jar test.jar &
  • Let me check this – Kushal Jain Oct 26 '17 at 19:34
  • what nohup exactly does ? and in my program I use logger. So, nohup is something related to this because it create a file nohup.out – Kushal Jain Oct 26 '17 at 19:39
  • From the attached link in my answer: "nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout. Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected." If you are logging your output to console then it will be printed in nohup.out file instead. However if you are using your own log file, then nothing will change. –  Oct 26 '17 at 19:43
  • Hey there is some issue, according to my program, it will terminate when i==50 but when I run it using nohup it goes running and running. – Kushal Jain Oct 26 '17 at 19:46
  • In simplier words: nohup will not "attach" the java program you run to your ssh session. At the time of disconnecting all processes attached to your session are being terminated. The ones started with nohup command will not because they are not part of it. –  Oct 26 '17 at 19:47
  • How do you check if the program is still running ? –  Oct 26 '17 at 19:50
  • Sorry, thats my mistake in the program. One more thing I need to ask, if I run using nohup and after sometime I want to terminate the program. How can I do it ? – Kushal Jain Oct 26 '17 at 19:52
  • 1
    Please refer to [link](https://stackoverflow.com/questions/17385794/how-to-get-the-process-id-to-kill-a-nohup-process) –  Oct 26 '17 at 19:55
1

You can look into using the screen utility on linux which continues to run after after you logout of your session.

https://www.linode.com/docs/networking/ssh/using-gnu-screen-to-manage-persistent-terminal-sessions

Sunshine
  • 439
  • 2
  • 6