-
Running a Python Script in the Background in Linux
-
You can run the script with
nohup which ignores the hangup signal. This means that you can close the terminal without stopping the execution.
Also, don’t forget to add & so the script runs in the background:
# test_handler.py
from handler import EndpointHandler
handler = EndpointHandler()
for _ in range(100):
(...)
response = handler(
data={
"base_image": base_image,
"material_image": material_image,
"prompt_material": prompt_material,
"seed": seed,
},
)
(...)
nohup python test_handler.py &
The output will be saved .log file:
nohup python test_handler.py > output.log &
[1] 1729
You can find the process and its process Id with this command:
ps aux | grep test_handler.py
root 1729 10.2 0.2 29499332 541108 pts/2 SNl 11:08 0:07 python test_handler.py
root 3007 0.0 0.0 4032 2008 pts/2 S+ 11:09 0:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox test_handler.py
You can kill it with the kill command with PID:
kill 1729