Run Python script on Terminal and keep using the terminal afterwards [duplicate] - python

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.

Related

How to run a Python process in background? [duplicate]

This question already has an answer here:
How to hide the python console window in Pyinstaller
(1 answer)
Closed 2 years ago.
I have a python script that is supposed to be running in the background. I used pyinstaller to create an executable file for the script. Now, this file opens up a terminal window. I don't have anything that is going to be printed on the terminal window, so I want it to close up just after starting up the process. How can I do that? Right now I just have a continous loop like this:
while True:
myFunction()
time.sleep(some-arbitrary-timeout-value)
Based on the context given, I will give you several options here for you to choose:
python pyinstaller.py --noconsole yourscript.py
You won't see the console window when executing your file.
nohup python your_executable.py & Keeps the script running even if you close the terminal
pythonw test.py similar to nohup, works on windows
you can launch your script and detach it from the terminal console by :
on linux you can use nohup & : nohup python your_executable.py &
on windows you can look at nohup on windows on stackoverflow

How do we create alias from python program/script? [duplicate]

This question already has an answer here:
create unix alias using a python3 script
(1 answer)
Closed 2 years ago.
I want to create an alias' through python program. I intend to use python as a replacement for shell scripting.
What I tried is:
import os
os.system('alias go2dir="cd /i/want/to/goto/this/dir"')
...and it does not work. I know the reason - that system command 'alias...' is getting executed in another shell and not in the current one where this python script is executed. So, that alias is not available to this shell.
What I don't know is - (In general,) how do we execute a command from a python program in the same shell where this python program is being executed. So that (in this case) the alias is available till the shell terminal is open?
The way other applications that want to automate actions in the user's shell work is that they write shell commands to their standard output. Then you can execute them with eval.
makealias.py:
print('alias go2dir="cd /i/want/to/goto/this/dir"')
Then in bash:
eval "$(python makealias.py)"
An example of a standard Unix program that works like this is tset with the -s option.

running python script in interactive python prompt and keep the variables? [duplicate]

This question already has answers here:
How to execute a file within the Python interpreter?
(12 answers)
Closed 5 years ago.
I understand from How do I run a Python program? that in command prompt i can use
C:\python>python first.py
, to run first.py.
But, is it possible, that after i entered the interactive python prompt, by runnning
C:\python>python
and see the >>> python indication, run first.py, and after finished running first.py, back to the interactive python prompt, I could see variables defined inside first.py?
For example, if first.py created some variables inside, e.g. by
(x,y) = [3,5]
, is it possible that after running first.py and back to the interactive python prompt, x and y are still there?
Running windows shell commands with python shows how to run the windows shell command in python, so in the interactive python prompt, i could actually use
>>>os.system('python first.py')
to run first.py, but x and y defined inside are lost after running.
Try the following for Python 2.x:
>>> execfile('first.py')
For Python 3.x, try this:
>>> exec(open("./first.py").read())
The variables should then be available to you.
Use
C:\python>python -i first.py
to run the script and get the interactive shell in the same namespace afterwards.

How can I run a python script in ubuntu background? [duplicate]

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

Python end program in shell [duplicate]

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

Categories

Resources