nohup和&的区别与关系

发布时间:2019-10-11 09:02:02编辑:auto阅读(1755)

    # test_nohup.py
    import time
    time.sleep(1000)
    print('test')

    & 是shell的命令,如果我们执行python test_nohup.py,就会直接返回shell给用户,且用户不能再进行输入。

    & puts the job in the background, that is, makes it block on attempting to read input, and makes the shell not wait for its completion.

    但如果我们关闭terminal,process将被关闭。只是失去了process从terminal获得输入的能力。

    (jd) ubuntu@vmXXX:~$ python3 test_nohup.py &
    [1] 11698

    nohup test_nohup.py

    nohup disconnects the process from the terminal, redirects its output to nohup.out and shields it from SIGHUP.

    我们仍然可以使用ctrl+c将进程(process)杀死,但如果我们关闭terminal,process仍然在后台进行。但我们无法立刻获得shell的交互能力。

    将两者结合起来,就能让程序在后台运行的同时,我们也能获得交互shell的能力。

    nohup python3 test_nohup.py > logfile.log &

    参考链接:
    http://unix.stackexchange.com...
    https://segmentfault.com/q/10...

关键字