399

I'm trying to port a shell script to the much more readable python version. The original shell script starts several processes (utilities, monitors, etc.) in the background with "&". How can I achieve the same effect in python? I'd like these processes not to die when the python scripts complete. I am sure it's related to the concept of a daemon somehow, but I couldn't find how to do this easily.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Artem
  • 6,420
  • 6
  • 26
  • 26
  • 2
    The really duplicated question is [How to launch and run external script in background?](http://stackoverflow.com/questions/1605520/how-to-launch-and-run-external-script-in-background). Cheers ;) – oHo Nov 13 '13 at 09:30
  • 1
    Hi Artem. Please accept [Dan's answer](http://stackoverflow.com/a/7224186/938111) because (1) more votes, (2) `subprocess.Popen()` is the new recommended way since 2010 (we are in 2015 now) and (3) the [duplicated question](http://stackoverflow.com/questions/1605520/how-to-launch-and-run-external-script-in-background) redirecting here has also an accepted answer about `subprocess.Popen()`. Cheers :-) – oHo Jun 26 '15 at 08:10
  • 1
    @olibre In fact the answer should be `subprocess.Popen("")` with file led by a suitable shebang. Works perfect for me (Debian) with bash and python scripts, implicitely `shell`s and survives its parent process. `stdout` goes to same terminal than the parent's. So this works much like `&` in a shell which was OPs request. But hell, all the questions work out very complex while a little testing showed it in no time ;) – flaschbier Jun 26 '15 at 18:54
  • For background maybe see also https://stackoverflow.com/a/51950538/874188 – tripleee Sep 18 '18 at 05:40

10 Answers10

505

While jkp's solution works, the newer way of doing things (and the way the documentation recommends) is to use the subprocess module. For simple commands its equivalent, but it offers more options if you want to do something complicated.

Example for your case:

import subprocess
subprocess.Popen(["rm","-r","some.file"])

This will run rm -r some.file in the background. Note that calling .communicate() on the object returned from Popen will block until it completes, so don't do that if you want it to run in the background:

import subprocess
ls_output=subprocess.Popen(["sleep", "30"])
ls_output.communicate()  # Will block for 30 seconds

See the documentation here.

Also, a point of clarification: "Background" as you use it here is purely a shell concept; technically, what you mean is that you want to spawn a process without blocking while you wait for it to complete. However, I've used "background" here to refer to shell-background-like behavior.

TheTechRobo the Nerd
  • 1,249
  • 15
  • 28
Dan
  • 12,157
  • 12
  • 50
  • 84
  • 19
    @Dan: How do I kill the process once it's running in the background? I want to run it for a while (it's a daemon that I interact with) and kill it when I'm done with it. The docs aren't helpful... – Juan Jul 07 '14 at 05:36
  • @Juan: If you're running on a unix system, you could use the `kill` command. You could also kill it from the task manager in windows. – Dan Jul 10 '14 at 20:50
  • 1
    @Dan but don't I need to know the PID for that? Activity monitor/Task manager not an option (needs to happen programmatically). – Juan Jul 12 '14 at 16:56
  • 5
    ok so how do you force the process to background when you need the result of Popen() to write to its stdin? – Michael Jul 19 '14 at 20:23
  • It is a misleading answer. "background" job is the property of a shell but `Popen()` does not use a shell to run commands by default. `stdout=PIPE` won't change it. – jfs Dec 16 '14 at 16:59
  • 2
    @J.F.Sebastian: I interpreted it as "how can I create an independent process that doesn't stop the execution of the current program". How would you suggest I edit it to make this more explicit? – Dan Dec 17 '14 at 04:51
  • 1
    @Dan: the correct answer is: use `Popen()` to avoid blocking the main thread and if you need a daemon then look at [`python-daemon` package](https://pypi.python.org/pypi/python-daemon/) to understand how a well-defined daemon should behave. Your answer is ok if you remove everything starting with "But be wary" except for the link to subprocess' docs. – jfs Dec 17 '14 at 14:06
  • @Dan are you sure that it's true that the last command will not run? AFAIK you can create a Popen object and access the `pid` property without blocking anything in the parent process.... see the answer by "f p" below (http://stackoverflow.com/a/13593257/405682) – benjaoming May 13 '15 at 15:09
  • 10
    @Dan `proc = subprocess.Popen(["rm","-r","some.file"])`, then to kill: `proc.terminate()` – A T Jun 09 '15 at 04:52
  • 1
    Actually `Popen(["ls", "-a"], stdout=subprocess.PIPE)` runs in the background. The problem is when you later use `p.communicate()` which brings the process to the foreground. – Pithikos Jul 05 '15 at 12:21
  • @Pithikos: do not use `stdout=PIPE` unless you read from the pipe while the process is running otherwise the child process may hang foreever if the corresponding OS pipe buffer fills up. – jfs May 24 '16 at 08:49
  • How should I do to integrate variables ? My command is something like this : command = 'python UploadService.py ' + var1 + ' ' + var2 + ' ' + var3 . Doing subprocess.Popen([command]) does not work – MouIdri May 24 '17 at 10:21
  • what if I want to run a command and then keep sending stuff to that process running in the background? – Charlie Parker Feb 23 '19 at 21:31
  • @Charlie Parker: See [the documentation on subprocess](https://docs.python.org/3/library/subprocess.html). If that doesn't help, you might want to ask a new question. – Dan Feb 23 '19 at 23:57
  • I've just edited this aggressively to fix errors that I noticed or were pointed out above by others. Hopefully you don't object to the changes. If you do, you of course have the power to rollback, but I'd appreciate if you'd also let me know why so we can discuss. If you're happy, might be worth flagging for the mods to clean up some of the now-obsolete comments in this thread (including this one). – Mark Amery Sep 26 '19 at 19:36
  • I am not sure why, but this technique does not work for me where I am trying `subprocess.Popen(["/some/path/to/hive","-f","/some/path/to_HQL/hql.dat"])` – Mark Ginsburg Sep 21 '20 at 18:14
  • @MarkGinsburg What exactly doesn't work about it? Does it block the rest of your script instead of running in parallel? I don't know much about how python's changed in the years since I wrote this, but a good workaround might be to also use `os.fork()` unixy systems. – Dan Sep 22 '20 at 04:32
  • I was wrong... it did launch but the job did not show up (in a Unix system) when I typed 'jobs' --- but it was visible when I did 'ps -elf | grep aStringContainedinMyJob' So this technique fooled me, because I expected it to show up in 'jobs'. – Mark Ginsburg Sep 23 '20 at 04:17
  • @MarkGinsburg: Yeah, this doesn't really run the job "in the background" (which is a shell concept), this creates another process that runs the command independently. The shell doesn't even know about it. This resembles the background in that the job runs in parallel with the main program without blocking it, which is more properly a forked process. If you want the process's information in the shell, you can print out the PID and use that to look at the process's info with `ps`. – Dan Sep 26 '20 at 07:28
  • I wonder why you suggest `communicate()` in place of `wait()`... – x-yuri Dec 06 '20 at 20:20
106

Note: This answer is less current than it was when posted in 2009. Using the subprocess module shown in other answers is now recommended in the docs

(Note that the subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using these functions.)


If you want your process to start in the background you can either use system() and call it in the same way your shell script did, or you can spawn it:

import os
os.spawnl(os.P_DETACH, 'some_long_running_command')

(or, alternatively, you may try the less portable os.P_NOWAIT flag).

See the documentation here.

hl037_
  • 3,520
  • 1
  • 27
  • 58
jkp
  • 78,960
  • 28
  • 103
  • 104
  • 9
    Remark: you must specify the full path to the executable. This function will not use the PATH variable and the variant that does use it is not available under Windows. – sorin Oct 28 '09 at 17:14
  • 40
    straight up crashes python for me. – pablo Feb 28 '11 at 13:58
  • 36
    os.P_DETACH has been replaced with os.P_NOWAIT. – oneself Jul 06 '12 at 19:28
  • 2
    From the docs: "Note that the subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using these functions" - use the subprocess answer below instead. – jtriley Jun 06 '13 at 22:18
  • os.spawn family can crash silently (for example, due to [this bug](http://bugs.python.org/issue8036)). See the `subprocess.Popen` and `subprocess.call` replacements for them: http://docs.python.org/2/library/subprocess.html#replacing-older-functions-with-the-subprocess-module – Oleksandr Fedorov Feb 06 '14 at 11:34
  • 17
    Could the people suggesting using `subprocess` give us a hint how to detach a process with `subprocess`? – rakslice Feb 11 '14 at 21:56
  • 3
    How can I use Python script (say attach.py) to find a background process and redirect its IO so that attach.py can read from / write to some_long_running_prog in background? – raof01 Mar 05 '14 at 06:46
  • what if I want to run a command and then keep sending stuff to that process running in the background? – Charlie Parker Feb 23 '19 at 21:32
  • 2
    In 2021 I get `ValueError: spawnv() arg 2 cannot be empty` from the code above. – Jortega Oct 28 '21 at 20:47
  • AttributeError: module 'os' has no attribute 'P_DETACH' – CS QGB May 02 '23 at 10:18
57

You probably want the answer to "How to call an external command in Python".

The simplest approach is to use the os.system function, e.g.:

import os
os.system("some_command &")

Basically, whatever you pass to the system function will be executed the same as if you'd passed it to the shell in a script.

Community
  • 1
  • 1
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
  • 12
    IMHO, python scripts are usually written to be cross-platform and if there simple cross-platform solution exists it's better to stick with it. Never know if you'll have to work with another platform in future :) Or if some other man would want to migrate your script to his platform. – d9k May 01 '16 at 02:37
  • 10
    This command is synchronous (i.e. it always waits for a termination of the started process). – tav Oct 07 '17 at 09:10
  • @d9k is os.system not portable? – lucid_dreamer Jun 04 '18 at 17:55
  • 1
    @d9k isn't the choice of running something in the background already positioning you in posix-land? What would you do on Windows? Run as a service? – lucid_dreamer Jun 04 '18 at 17:57
  • how can I use this if I need to run a command from a specific folder? – mrRobot Nov 16 '19 at 16:14
  • @mrRobot: see https://stackoverflow.com/a/431715/1694 for how to change the current working directory of the current Python script, which you can do immediately before executing the sub-command. – Eli Courtwright Nov 18 '19 at 18:45
  • This will not work in windows. Eg, a launched window would close when script is done. – Timothy C. Quinn Jan 17 '22 at 23:17
  • @mrRobot: you can use os.system("cd somedir && some_command &") – Ben L Aug 24 '22 at 18:07
40

Use subprocess.Popen() with the close_fds=True parameter, which will allow the spawned subprocess to be detached from the Python process itself and continue running even after Python exits.

https://gist.github.com/yinjimmy/d6ad0742d03d54518e9f

import os, time, sys, subprocess

if len(sys.argv) == 2:
    time.sleep(5)
    print 'track end'
    if sys.platform == 'darwin':
        subprocess.Popen(['say', 'hello'])
else:
    print 'main begin'
    subprocess.Popen(['python', os.path.realpath(__file__), '0'], close_fds=True)
    print 'main end'
JHobern
  • 866
  • 1
  • 13
  • 20
Jimmy KD
  • 633
  • 7
  • 14
  • 1
    In windows, it doesn't detach but using creationflags parameter works – Smart Manoj Apr 26 '18 at 15:43
  • 5
    This solution leaves a subprocess as Zombie on Linux. – TitanFighter Apr 03 '19 at 14:24
  • @TitanFighter this can be avoid by set SIGCHLD SIG_IGN : https://stackoverflow.com/questions/16807603/python-non-blocking-non-defunct-process – sailfish009 May 30 '20 at 05:20
  • 2
    thanks @Jimmy your answer is the ONLY solution works for me. – sailfish009 May 30 '20 at 05:23
  • The `close_fds=True` option works by detaching the process, but it didn't return back to my Python program. Hoping to find an option that truly executes a process and sends it to the background and then returns back to the Python program. – Nav Apr 27 '21 at 07:27
39

I found this here:

On windows (win xp), the parent process will not finish until the longtask.py has finished its work. It is not what you want in CGI-script. The problem is not specific to Python, in PHP community the problems are the same.

The solution is to pass DETACHED_PROCESS Process Creation Flag to the underlying CreateProcess function in win API. If you happen to have installed pywin32 you can import the flag from the win32process module, otherwise you should define it yourself:

DETACHED_PROCESS = 0x00000008

pid = subprocess.Popen([sys.executable, "longtask.py"],
                       creationflags=DETACHED_PROCESS).pid
rstackhouse
  • 2,238
  • 24
  • 28
f p
  • 3,165
  • 1
  • 27
  • 35
16

Both capture output and run on background with threading

As mentioned on this answer, if you capture the output with stdout= and then try to read(), then the process blocks.

However, there are cases where you need this. For example, I wanted to launch two processes that talk over a port between them, and save their stdout to a log file and stdout.

The threading module allows us to do that.

First, have a look at how to do the output redirection part alone in this question: Python Popen: Write to stdout AND log file simultaneously

Then:

main.py

#!/usr/bin/env python3

import os
import subprocess
import sys
import threading

def output_reader(proc, file):
    while True:
        byte = proc.stdout.read(1)
        if byte:
            sys.stdout.buffer.write(byte)
            sys.stdout.flush()
            file.buffer.write(byte)
        else:
            break

with subprocess.Popen(['./sleep.py', '0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc1, \
     subprocess.Popen(['./sleep.py', '10'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc2, \
     open('log1.log', 'w') as file1, \
     open('log2.log', 'w') as file2:
    t1 = threading.Thread(target=output_reader, args=(proc1, file1))
    t2 = threading.Thread(target=output_reader, args=(proc2, file2))
    t1.start()
    t2.start()
    t1.join()
    t2.join()

sleep.py

#!/usr/bin/env python3

import sys
import time

for i in range(4):
    print(i + int(sys.argv[1]))
    sys.stdout.flush()
    time.sleep(0.5)

After running:

./main.py

stdout get updated every 0.5 seconds for every two lines to contain:

0
10
1
11
2
12
3
13

and each log file contains the respective log for a given process.

Inspired by: https://eli.thegreenplace.net/2017/interacting-with-a-long-running-child-process-in-python/

Tested on Ubuntu 18.04, Python 3.6.7.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
13

You probably want to start investigating the os module for forking different threads (by opening an interactive session and issuing help(os)). The relevant functions are fork and any of the exec ones. To give you an idea on how to start, put something like this in a function that performs the fork (the function needs to take a list or tuple 'args' as an argument that contains the program's name and its parameters; you may also want to define stdin, out and err for the new thread):

try:
    pid = os.fork()
except OSError, e:
    ## some debug output
    sys.exit(1)
if pid == 0:
    ## eventually use os.putenv(..) to set environment variables
    ## os.execv strips of args[0] for the arguments
    os.execv(args[0], args)
1

You can use

import os
pid = os.fork()
if pid == 0:
    Continue to other code ...

This will make the python process run in background.

0

I haven't tried this yet but using .pyw files instead of .py files should help. pyw files dosen't have a console so in theory it should not appear and work like a background process.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31033176) – padaleiana Feb 15 '22 at 12:12
0

Unlike some prior answers that use subprocess.Popen, this answer uses subprocess.run instead. The issue with using Popen is that if the process is not manually waited for until completion, a stale <defunct> entry remains in the Linux process table as seen by ps. These entries can add up.

In contrast, with subprocess.run, by design it waits for the process to complete, and so no such defunct entry will remain in the process table. Because subprocess.run is blocking, it can be run in a thread. The rest of the code can continue after starting this thread. In this way, the process effectively runs in the background.

import subprocess, threading

kwargs = {stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True, **your_kwargs}
threading.Thread(subprocess.call, args=(your_command,), kwargs=kwargs).start()
Asclepius
  • 57,944
  • 17
  • 167
  • 143