428

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.

3
  • 3
    The really duplicated question is How to launch and run external script in background?. Cheers ;) Commented Nov 13, 2013 at 9:30
  • 1
    @olibre In fact the answer should be subprocess.Popen("<command>") with <command> file led by a suitable shebang. Works perfect for me (Debian) with bash and python scripts, implicitely shells 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 ;) Commented Jun 26, 2015 at 18:54
  • For background maybe see also stackoverflow.com/a/51950538/874188 Commented Sep 18, 2018 at 5:40

11 Answers 11

531

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.

Sign up to request clarification or add additional context in comments.

21 Comments

@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...
@Dan but don't I need to know the PID for that? Activity monitor/Task manager not an option (needs to happen programmatically).
ok so how do you force the process to background when you need the result of Popen() to write to its stdin?
@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 proc = subprocess.Popen(["rm","-r","some.file"]), then to kill: proc.terminate()
|
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.

10 Comments

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.
straight up crashes python for me.
os.P_DETACH has been replaced with os.P_NOWAIT.
Could the people suggesting using subprocess give us a hint how to detach a process with subprocess?
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?
|
60

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.

10 Comments

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.
This command is synchronous (i.e. it always waits for a termination of the started process).
@d9k is os.system not portable?
@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?
how can I use this if I need to run a command from a specific folder?
|
44

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.

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' 

5 Comments

In windows, it doesn't detach but using creationflags parameter works
This solution leaves a subprocess as Zombie on Linux.
@TitanFighter this can be avoid by set SIGCHLD SIG_IGN : stackoverflow.com/questions/16807603/…
thanks @Jimmy your answer is the ONLY solution works for me.
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.
43

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 

4 Comments

+1 for showing how to retain the process id. And if anyone want to kill the program later with the process id: stackoverflow.com/questions/17856928/…
This seems Windows only
any cross-platform solution for this?
ValueError: creationflags is only supported on Windows platforms
18

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.

Comments

14

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) 

4 Comments

os.fork() is really useful, but it does have a notable downside of only being available on *nix.
The only problem with os.fork is that it is win32 specific.
More details about this approach: Creating a daemon the Python way
You can also reach similar effects with threading: stackoverflow.com/a/53751896/895245 I think that might work on Windows.
3

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 to Popen, when using subprocess.run, by design run 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.run, args=(your_command,), kwargs=kwargs).start() 

Note that subprocess.call also waits for the process to complete, and can be used similarly.

Comments

2

You can use

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

This will make the python process run in background.

Comments

1

I'm running Python 3.9.14 on Linux. I found the following worked for me in a similar situation:

import subprocess cmd = "sleep 5 && ls /tmp >& ls.out &" try: runResult = subprocess.run(["bash", "-c", cmd]) except Exception as ex: print( f"Failed to run '{cmd}'" ) if hasattr( ex, "message" ): print( ex.message ) elif hasattr( ex, "strerror" ): print( ex.strerror) else: print( ex ) 

If you run the above and quickly do an ls in the current directory, you will find that the "ls.out" file doesn't yet exist. Wait a few more seconds and the file is there. So, the command continues to run after Python exits.

The 'runResult' has a 'returncode' field that indicates whether the program launched successfully or not. I do not know of a good way of later killing the process from within Python.

I was also able to do a more Python-ish (Python-ly?) approach: I have a shell script named "runs5secs":

#!/bin/bash sleep 5 ls 

and I can run it in the background with:

import shlex import subprocess cmd = "sleep 5 && ls /tmp >& ls.out &" logName = "./run5secs.out" cmd = "./run5secs my1 your2" try: f = open( logName, 'w' ) except Exception as ex: print( f"Failed to run '{cmd}'" ) if hasattr( ex, "message" ): print( ex.message ) elif hasattr( ex, "strerror" ): print( ex.strerror) else: print( ex ) args = shlex.split( cmd ) try: cmdRes = subprocess.Popen( args, stdout=f, stderr=subprocess.STDOUT, universal_newlines=True ) except Exception as ex: print( f"Failed to run '{cmd}'" ) if hasattr( ex, "message" ): print( ex.message ) elif hasattr( ex, "strerror" ): print( ex.strerror) else: print( ex ) print( cmdRes ) 

Comments

-1

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.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.