Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4412451

Shutdown Hook called when nohup is used to launch process

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0, 1.4.0
    • core-libs
    • sparc
    • solaris_8

      Name: boT120536 Date: 02/06/2001


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
      Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)

      Script and source code are at the end of this message.

      Steps:

      Scenario 1: With Shutdown Hook registered

      1. Compile the file, ShutdownHookTest.java
      2. In CDE File Manager, right click on runShutdownHookTest, and select RunWithOptions
      3. type in "true" in the Options text box
      4. click "OK" button
      5. After you see the RunWithOptions window open and the JFrame display, close
          the RunWithOptions window (double click on top left corner of window)
      6. Notice that the JFrame disappeared, and the program has exited. The
          program should not have exited when the RunWithOptions window was closed.

      Scenario 2: Without ShutdownHook registered

      1. Compile the file, ShutdownHookTest.java
      2. In CDE File Manager, right click on runShutdownHookTest, and select RunWithOptions
      3. type in "false" in the Options text box
      4. click "OK" button
      5. After you see the RunWithOptions window open and the JFrame display, close
          the RunWithOptions window (double click on top left corner of window)
      6. Notice that the JFrame remains. This is the proper behavior for nohup.

      For some reason, the ShutdownHook bypasses the nohup on Solaris. This is the
      bug I am reporting.

      runShutdownHookTest script:
      ----
      #!/bin/sh
      # script to run java program with nohup
      #
      nohup java ShutdownHookTest $* &
      echo done
      ----

      Source code for ShutdownHookTest.java:
      ----
      import javax.swing.JFrame;

      /**
       * Test the ShutdownHook. This program will demonstrate that when it is
       * launched via a script (with nohup) from the Solaris CDE file manager, when
       * the shell window that launched the program closes, the shutdown hook is
       * called and the program is terminated. If the shutdown hook is not
       * registered, the program does not terminate when the window is closed,
       * which is the expected behavior.
       *
       * Apparently, the shutdown hook gets called even though nohup is used
       * in the script.
       * @author Maurice Ling (###@###.###)
       **/
      public class ShutdownHookTest extends Thread
      {
        ShutdownHookTest(boolean addShutdownHook)
        {
          System.out.println("Starting up...");
          if (addShutdownHook)
          {
            System.out.println("Registering Shutdown Hook");
            Runtime.getRuntime().addShutdownHook(this);
          }
        }

        /**
         * Thread run method
         **/
        public void run()
        {
          System.out.println("Executing ShutdownHook...");
        }

        public static void main(String argv[])
        {
          if (argv.length != 1)
          {
            System.out.println("Usage: java ShutdownHookTest {true|false}");
            System.exit(-1);
          }

          JFrame frame = new JFrame();

          // show a frame to show that this application is alive
          frame.show();

          boolean flag = (argv[0].equals("true")?true:false);
          ShutdownHookTest test = new ShutdownHookTest(flag);

          try
          {
            // wait forever...
            Thread.currentThread().join();
          } catch (InterruptedException ie)
          {
          }
          System.out.println("Done");
        }
      }
      ----
      (Review ID: 116312)
      ======================================================================

      Name: yyT116575 Date: 03/05/2001


      java version "1.3.0_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_01)
      Java HotSpot(TM) Client VM (build 1.3.0_01, mixed mode)

      A 'normal' user uses nohup to start a process that adds a shutdown hook. The
      user starts viewing 'nohup.out' and after a while stops the viewing by pressing
      Ctrl-C. The process is still running normally.

      When a root user starts the same proces using nohup, and stops the viewing of
      the output by pressing Ctrl-C, the process calls the shutdown hook and the
      process is killed.

      runShutdownHookTest script:
      --------------------------------------------------------------------------------
      nohup /opt/jdk1.3.0/bin/java ShutdownHookTest > out &
      --------------------------------------------------------------------------------

      source code for ShutdownHookTest.java:
      --------------------------------------------------------------------------------
      /**
       * Object responsible for testing problems with the shutdown hook.
       *
       * <h3>Purpose and Responsibilities</h3>
       *
       * It seems when a normal user starts this little application as a nohup job
       * and view the output using tail -f, then after a while stops viewing the
       * output, the job continues normally.
       * <p>
       * Whenever a root user starts the same job and view the output and after a
       * stops the viewing of the output, the job (JVM) stops completely. This only
       * happens whenever a shutdownhook has been added.
       *
       * @author Karel Mentink
       * @version $Revision: #1 $ $Date: 2001/03/05 $
       */
      public class ShutdownHookTest extends Thread {

        /**
         * Default constructor.
         */
        public ShutdownHookTest() {
          System.out.println("Start test");
          Runtime.getRuntime().addShutdownHook(this);
        }

        /**
         * Actual method being called during shutdown of JVM.
         */
        public synchronized void run() {
          System.out.println("The test application has been shutdown.");
        }

        /**
         * Main.
         */
        public static void main(String[] args) {
          ShutdownHookTest test = new ShutdownHookTest();

          for ( ; true ; ) {
            System.out.println(System.currentTimeMillis());

            try {
              Thread.currentThread().sleep(1000);
            }
            catch (InterruptedException ie) {
              // ignore
            }
          }
        }
      }
      --------------------------------------------------------------------------------
      (Review ID: 118124)
      ======================================================================

            chegar Chris Hegarty
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: