1

In bash, I can do the following:

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

in other words, it runs all *.sh scripts in subdir in the background, and detached so that if the main script ends, the background scripts won't be terminated.

Now, let's say I have the following Python project:

proj/
    __init__.py
    main.py
    subdir/
        __init__.py
        mod_a.py
        mod_b.py
        mod_c.py

How do I do something similar to the bash script? But with parameters passed as Python objects?

E.g.: I have two strings a and b, a list l, and a dictionary d

  • Load mod_a.py, invoke mod_a.main(a, b, l, d), and detach
  • Load mod_b.py, invoke mod_b.main(a, b, l, d), and detach
  • Load mod_c.py, invoke mod_c.main(a, b, l, d), and detach
  • main.py can end, letting mod_a, mod_b, and mod_c run in the background until completion
pepoluan
  • 6,132
  • 4
  • 46
  • 76

3 Answers3

1

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

I don't know about any mechanism for it in python, but you may try to use nohup. You may try to run

nohup python your_script.py arguments

Using os.system or subprocess.call.

Nebril
  • 3,153
  • 1
  • 33
  • 50
0

It's probably a duplicate of Run a program from python, and have it continue to run after the script is killed and no, ignoring SIGHUP doesn't help, but preexec_fn=os.setpgrp does.

Community
  • 1
  • 1
proski
  • 3,603
  • 27
  • 27
  • [`nohup`](http://src.gnu-darwin.org/src/usr.bin/nohup/nohup.c.html) does not create a new process group. `preexec_fn=os.setsid` is not enough to create a completely detached subprocess too. See [correct daemon behaviour](https://www.python.org/dev/peps/pep-3143/#correct-daemon-behaviour). – jfs Jan 05 '15 at 08:20