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\"'")
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:
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
This question already has answers here:
Using sudo with Python script
(14 answers)
Closed 7 years ago.
I have to open a terminal using sudo from python. Consider my password is pass and I need to run a command within script which is sudo critical-stack-intel pull.
I have following small piece of code:
import subprocess
import shlex
command = "sudo critical-stack-intel pull"
popen = subprocess.Popen(shlex.split(command),stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
popen.communicate("pass")
popen.wait()
# print help(p)
If i run the file as python myfile.py, it asks me for password within terminal. This is not what I desire. I want the python to handle the password I gave and run normally. How do I get this done?
EDIT
Using popen.communicate(pass + "\n") along with sudo -S did what i desired.
You can use the -S option of sudo to pass the password via stdin. Most likely, though, it's a better idea to allow sudo access to critical-stack-intel without password using /etc/sudoers.
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 answers here:
How do I execute a program or call a system command?
(65 answers)
Running a linux command from python
(5 answers)
Closed 8 years ago.
how can I execute the following command in python please
sudo mount --bind /media/networkshare/camera /var/www/media
Technically you could use Python's subprocess module for this (see also this answer):
import subprocess
subprocess.check_call(['sudo', 'mount', '--bind', '/media/networkshare/camera',
'/var/www/media'])
Of course, this will still prompt you for your password. If you don't want it to prompt for a password, then you'll have to setup sudo so that it can execute a single command as root. See the following guide for how to do that:
https://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password