How to spawn a new independent process in Python

前端 未结 2 1632
猫巷女王i
猫巷女王i 2020-12-15 04:10

I have a some Python code that occasionally needs to span a new process to run a shell script in a \"fire and forget\" manner, i.e. without blocking. The shell script will n

2条回答
  •  一个人的身影
    2020-12-15 04:48

    Try prepending "nohup" to script.sh. You'll probably need to decide what to do with stdout and stderr; I just drop it in the example.

    import os
    from subprocess import Popen
    
    devnull = open(os.devnull, 'wb') # Use this in Python < 3.3
    # Python >= 3.3 has subprocess.DEVNULL
    Popen(['nohup', 'script.sh'], stdout=devnull, stderr=devnull)
    

提交回复
热议问题