我将java程序写到一个shell文件中,并且让java程序在后台执行( java yourFile & ),这样关掉shell后,java程序不会被关掉

解决方案 »

  1.   

    1简单java MyExample &
    加了&表示后台运行2的方式太多了
    涉及到进程间通讯的内容
      

  2.   

    谢谢大家的回答,我的测试结果是:
    “后台运行”表示可以在当前shell执行其它命令,但关掉linux shell(或telnet的dos窗口)后,此进程就关掉了。我的测试过程:
    [root@localhost test]# ./javac.sh ScheduleBashRun.java
    [root@localhost test]# ./java.sh ScheduleBashRun &
    结果我关掉“telnet的dos窗口”后,ScheduleBashRun(包括其启动的线程)都关掉了。测试源代码:
    javac.sh
    /usr/java/j2sdk1.4.0/bin/javac $1 $2 $3java.sh
    /usr/java/j2sdk1.4.0/bin/java $1 $2 $3ScheduleBashRun.java
    import java.io.FileOutputStream;
    import java.io.OutputStreamWriter;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.Toolkit;
    public class ScheduleBashRun {
        Timer timer;
        
        public ScheduleBashRun(int milliseconds) {
            timer = new Timer();
            timer.schedule(new RemindTask(), 0, milliseconds);
        }
        class RemindTask extends TimerTask {
         int countSchedule=20;
         FileOutputStream fos = null;
         OutputStreamWriter outkk = null;
         public RemindTask() {
         try {
         this.fos = new FileOutputStream("result.txt");
    this.outkk = new OutputStreamWriter(fos);
    } catch(Exception e) {
             e.printStackTrace();
            }
    }
            public void run() {
                //System.out.println("left count:" + this.countSchedule);
         if( countSchedule<1 ) {
         try {
         this.outkk.close();
         this.fos.close();
         } catch(Exception e) {
             e.printStackTrace();
            }
            System.exit(0);
         } else {
         try {
         this.outkk.write("test" + this.countSchedule + "\r\n");
         this.countSchedule--;
            } catch(Exception e) {
             e.printStackTrace();
            }
         }
            }
        }    public static void main(String args[]) {
    System.out.println("About to schedule task.");
    new ScheduleBashRun(1000);
    System.out.println("Task scheduled.");
        }
    }