Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

start python script as background process from within a python script [duplicate]

Tags:

python

My python script needs to start a background process and then continue processing to completion without waiting for a return.

The background script will process for some time and will not generate any screen output.

There is no inter-process data required.

I have tried using various methods subprocess, multiprocessing but am clearly missing something.

Does anyone have a simple example?

TIA

like image 984
user434315 Avatar asked Aug 29 '10 17:08

user434315


People also ask

How can I auto run a Python script in background?

The easiest way of running a python script to run in the background is to use cronjob feature (in macOS and Linux). In windows, we can use Windows Task Scheduler. You can then give the path of your python script file to run at a specific time by giving the time particulars.

Can you run the same Python script twice?

You can run multiple instances of a python script from a shell however from within a python program without the use of multithreading/multiprocessing the GIL limitation will impact what you are trying to do.

How do you run a Python script in the background even after I log out?

Role of nohup : nohup makes your script ignore SIGHUP , and redirects stdout/stderr to a file nohup. out, so that the command can continue running in the background after you log out. If you close the shell/terminal or log off, your command is no longer a child of that shell. It belongs to init process.


1 Answers

how about this:

import subprocess
from multiprocessing import Process

Process(target=subprocess.call, args=(('ls', '-l', ), )).start()

It's not all that elegant, but it fulfils all your requirements.

like image 165
Stefano Palazzo Avatar answered Oct 11 '22 17:10

Stefano Palazzo