Launch modules as subprocesses in the background, and detach

前端 未结 3 2074
借酒劲吻你
借酒劲吻你 2021-01-25 07:57

In bash, I can do the following:

for f in subdir/*.sh; do
    nohup \"$f\" \"$@\" &> /dev/null &
done

in other words, i

3条回答
  •  旧巷少年郎
    2021-01-25 08:52

    To emulate nohup in Python, you could make child processes to ignore SIGHUP signal:

    import signal
    
    def ignore_sighup():
        signal.signal(signal.SIGHUP, signal.SIG_IGN)
    

    i.e., to emulate the bash script:

    #!/bin/bash
    for f in subdir/*.sh; do
        nohup "$f" "$@" &> /dev/null &
    done
    

    using subprocess module in Python:

    #!/usr/bin/env python3
    import sys
    from glob import glob
    from subprocess import Popen, DEVNULL, STDOUT
    
    for path in glob('subdir/*.sh'):
        Popen([path] + sys.argv[1:], 
              stdout=DEVNULL, stderr=STDOUT, preexec_fn=ignore_sighup)
    

    To create proper daemons, you could use python-daemon package.

提交回复
热议问题