This question already has answers here:
How to run a script in the background even after I logout SSH?
(12 answers)
Closed 5 years ago.
How can I run a python script in ubuntu background? I tried to use '&', for example:
python3 test.py &
but when I close the terminal, this process seems to be closed as well because I can't get any update logs from this test script any more.
You can use setsid. In your case by running:
setsid python test.py
Or, as mentioned in the comments, you can use nohup.
nohup python test.py
You can see the difference between them in this answer: What's the difference between nohup and a daemon?
I think you're looking for the nohup command as Serge mentioned.
This answer looks like what you want
Related
This question already has an answer here:
run a process to /dev/null in python
(1 answer)
Closed 2 years ago.
I am trying to run something inside a Python file, and i do not want to see in the Python console the run output:
os.system("cd ../programs/{0} && ./run".format(project))
How can I do it? I tried with subprocess.call() but it does not compile.
Redirecting stdout to /dev/null:
import subprocess
subprocess.call(['./run'],
cwd=os.path.join('..', 'programs', project),
stdout=subprocess.DEVNULL)
This question already has answers here:
Run a shell script and immediately background it, however keep the ability to inspect its output
(3 answers)
Closed 4 years ago.
I have a script that I activate via the terminal that listens to events. I want to keep using the terminal after the script runs, but the script is running and I can't type anything. The only way I know that lets me keep typing is by using the Ctrl + C combination, but this stops the script.
So how can I run the script in the background and keep using the terminal without terminating it?
Edit: I tried to use the '&' operator but it didn't work:
You can run your script in the background with:
python myscript.py &
If your script does output something you can suppress the output with:
python myscript.py 1>/dev/null 2>&1 &
Or save the output for later:
python myscript.py 1>myoutputfile 2>&1 &
# ...
less myoutputfile
Another alternative would be to use for example screen or tmux.
This question already has answers here:
Python not working in the command line of git bash
(20 answers)
Closed 5 years ago.
I'm experiencing the same issue as this question with Python 3.6. It works well under the Windows console, but it fails to launch or work under MINGW64. Even when I use the direct path of /c/Python36/python.exe, nothing happens. I don't get the interactive REPL with the usual Python interpreter version and >>> prompt, as I do in the Windows console.
Any pointers on how to debug this?
Ah - further search yielded this stackoverflow question that contained the answer - use python -i
This question already has answers here:
Interacting with program after execution
(3 answers)
Closed 7 years ago.
I want a script to run an then finish on the python shell with all variables and methods:
$ python myprogram.py
...
program output
...
>>>
And with #!/usr/bin/python is posible? so I double-click and it just works?
Sounds like you want Python's i flag. From the help menu:
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
So the full command would be
python -i yourscriptname.py
This question already has an answer here:
Communication between two gnome-terminal sessions
(1 answer)
Closed 7 years ago.
Through python, is there a way I can open gnome-terminal and then send commands to it, which are then run in that window? For example, where I could do something like
terminal.communicate("echo testing")
and the gnome-terminal prints the output? I've seen similar posts using subprocess Popen and communicate, although I wasn't getting the newly opened terminal to run the commands. Thanks for any help
you could do os.system to execute commands, like This
import os
os.system("gnome-terminal -e 'bash -c \"sudo apt-get update; exec bash\"'")