I am running a program which allows me to run terminal commands through my Python code which takes input from the user through the command line. This is the part of the code where I open Google-Chrome
import sys
import os
os.system("google-chrome") #I have Ubuntu 16.04
It opens the browser but the problem is that the terminal on which my python code is running becomes the same as the one where Chrome is running which means that I cannot give further input to my Python code. To solve this problem I need the Chrome to run as a process on a different terminal. I tried using subprocess.call("google-chrome", shell=True) but it did not open it on a new terminal.
How do I make the process run on a different terminal?
can this solve your problem?
os.system('gnome-terminal -x chromium-browser')
Use subprocess.popen("command")
Basically, run the subprocess in the background. & is a shell feature. Use popen instead
Related
OS: Raspbian
Python: 3.7.3
I am trying to run and kill my shell script through a Python script. The purpose is so that I can simply press "Run" in my py script, instead of having to go through the terminal every time. Here is my shell script (T.sh):
#!/bin/bash
#Track command
cd /home/pi/rpi-deep-pantilt
. . /rpi-deep-pantilt-env/bin/activate
rpi-deep-pantilt track Raspi --edge-tpu
Here is my Py script:
import os
os.system('bash /home/pi/T.sh')
When I issue the command rpi-deep-pantilt track Raspi --edge-tpu in my terminal and press CTRL + C it kills the script, but it doesn't work when I use this Python script, and neither does pkill. The Python script stops, but the camera stays on and the tracking functionality is still operating. Is there any way I can incorporate some kill command that I can issue with a key interruption?
If there is a better way to go about this let me know. I'm very new to this as you can probably tell.
Python may create a new process for T.sh, so within your python code, try:
os.system("pkill T.sh")
I am trying to run a terminal command using python but i get a terminal error:
"x-terminal-emulator has very limited support, consider choose another terminal"
I am trying to change my desktop background using python. I have a raspberry pi running latest raspbian.
Running the command directly in lxterminal it works and changes my background:
"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge_wallpaper.jpg"
and changes my desktop background.
Using os.system("ls -a") works fine. As do other linux system commands such as "mkdir" or "pwd" python has no problem showing me the output in terminal.
I have tried using subprocess call functions nothing has worked so far.
I also have tried making python open a new, different terminal and running the code in the new terminal but also no luck:
os.system('gnome-terminal --command="pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge_wallpaper.jpg"')
Any thoughts guys? Do i have to start a seperate completely new terminal session? Is it a problem with my syntax? Am i using the wrong python command to execute command in a terminal?
import os,random
random_pic = random.choice(os.listdir('/usr/share/rpd-wallpaper/'))
shell_command = ("pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/" + str(random_pic))
os.system(shell_command)
print("success... wallpaper is changed")
I'm using LXDE, I would like to Launch a python script in a new terminal from another python script.
I would like the new python script to be totally independent.
I have tried a lot of things...
Calling xterm (or x-terminal-emulator) directly from python with the subprocess.call or subprocess.Popen with or without shell=True argument, it didn't work. It gives me an error about display not being set.
I have also created a sh file which calls the other python script and tried to call it, same results.
Any way to do this?
This works fine for me:
blocking:
import os
os.system("xterm -e \"python christmaskittens.py\"")
non blocking:
import os
os.system("xterm -e \"python christmaskittens.py\" &")
I have encountered a problem with my python script. I am using python 3.6.2 I am using the os.system function to execute a bash command. The problem is the script launches the bash command and continues without waiting it to finish, knowing that the script acts correctly in my physical machine, it waits till the bash command is done then continues with the following instructions. This is my script:
import os
os.system("C:/Bash/bashCmd.sh")
print("ok")
I also tried subprocess.call, but it never waits in my vm.
My question is about the source of this issue, then if any suggestions to deal with it!
I am using dryscrape in a python script. The python script is called in a bash script, which is run by cron. For those who may not be aware, dryscrape is a headless browser (use QtWebkit in the background - so requires an xsession).
Here are the main points concerning the issue I'm having
When I run the python script from the command line, it works
When I run the bash script from the command line, it works too
I figured out that this may have something to do with different environments between my command prompt and when the cron job is running, so I modified my bash script to source my .profile as follows:
#/bin/bash
. /full/path/to/my/home/directory/.profile
python script_to_run.py
This is what my cronjob crontab entry looks like:
0,55 14-22 * * 1-5 /path/to/script.sh >> $(date "+/path/to/logs/\%Y\%m\%d.mydownload.log" )
By the way, I know that the job is being run (I can see entries in /var/log/syslog, and the script also writes to a log file - which is where I get the error message below):
In all cases, I got the following error message:
Could not connect to X server. Try calling dryscrape.start_xvfb()
before creating a session
I have installed the prerequisites, on my machine (obviously - since it runs at the command line). At the moment, I have run out of ideas.
What is causing the script to run fine at the console, and then fail when run by cron?
[[Relevant Details]]
OS: Linux 16.0.4 LTS
bash: version 4.3.46(1)
cron user: myself (i.e. same user at the command prompt)
dryscrape: version 1.0.1
The solution to this was to call the dryscrape.start_xvfb() method before starting the dryscrape session.
Cron user does not have display, so you cannot run any command which requires a display.
You need to modify the python script to do not use any type of display (check carefully, because some python commands, even though they do not open any display , they internally check for this variable).
The best way to test is to ssh into the machine without Display, and check if you can run it from there without erros.