Linux kill & Java shutdownhook

2018-08-27  本文已影响0人  landon30

shutdown-hook

kill

linux

测试kill与Java#shutdownhook与nohup

public class ShutdownHookTest {
    public static void main(String[] args) throws Exception {
        Runtime.getRuntime()
                .addShutdownHook(new Thread(() -> System.out.println("ShutdownHookTest")));

        while (true) {
            Thread.sleep(5000);
        }
    }
}
    Historical versions of nohup did not affect standard input, but that causes problems in the common scenario where the user logs into a system, types the command:
    
    nohup make &
    
    at the prompt, and then logs out. If standard input is not affected by nohup, the login session may not terminate for quite some time, since standard input remains open until make exits. To avoid this problem, POSIX.1-2008 allows implementations to redirect standard input if it is a terminal. Since the behavior is implementation-defined, portable applications that may run into the problem should redirect standard input themselves. For example, instead of:
    
    nohup make &
    
    an application can invoke:
    
    nohup make </dev/null &
The redirection of stdin for nohup is entirely optional as nohup will usually redirect it to </dev/null if it was a terminal, to avoid the terminal not being able to close when you type exit. You can do your own 0< redirection, to /dev/null or a file, to avoid this.

上一篇下一篇

猜你喜欢

热点阅读