I am executing a python script in background mode from a shell script.
The script is running fine but I am unable to get the prompt back until I manually press enter from keyboard. I want to avoid the manual interaction.
I already tried running the script like echo "/n" | python script.py &
I also tried:
python3 /simulators/utils/reboot_ap.py $input 2>&1 &`
python3 script.py $input 2>&1 &
Related
I am attempting to get a subprocess call that will open a gnome-terminal and in that terminal enter python then execute some python commands and imports without the user needing to type them out.
I'm working on on some automated terminal opening code that will open a gnome-terminal window using subprocess.call (Open new gnome-terminal and run command) also (Python syntax to open gnome-terminal and execute multiple commands)
My end goal is to open up a gnome-terminal window and with the same script that opened the gnome-terminal, enter the command to use python. And then in python import a package and run it.
My current usage is:
subprocess.call(['gnome-terminal', '-e', "python client.py"])
However what Im trying to get to is an importable package that I can open several gnome terminal windows for that will call different objects from a pypi package effectively doing the same thing that call client.py would do with the files. This doesnt work with packages installed in pip however.
What I want to do is something along the lines of:
subprocess.call(['gnome-terminal', '-e', "python && import <package> && c = <package>.obj.func()"])
So that a terminal would open and enter python, import the package I want, then call something from it, but all as instructed by a python file
This doesnt appear to work as multiline scripting works for stuff like bash scripting but doesnt seem to work when trying to enter commands after python has been entered.
Any advice would be greatly appreciated
I don't have Gnome Terminal installed, but if you can get that to start Python correctly, then you can use Python's -i flag to run a set of commands or a script.
The two usages are as follows:
python -i path/to/my/script run the script then enter the interpreter
python -i -c "# Some Python commands" run the command(s) then enter the interpreter
For example:
$ python -i -c "import this"
[poetry]
>>>
# Ready for input!
I have a python script script.py
from time import sleep
for i in range(30):
print(i)
sleep(1)
I wrap this script into bash script script.sh
#!/bin/bash
python3 python_test.py
I want to run the bash script with nohup and redirect the output to output.out. Thus I run the linux command:
nohup bash script.sh > output.out &
However, the output is redirected to output.out only when the python script 'script.py' ends, not in the online manner. Thus the question.
Question. How to redirect the output to output.out into online manner?
Im trying to get python to run a terminal command which will change my desktop wallpaper. Running this command in a normal terminal will change my wallpaper. However when i try to make python run this command in terminal it doesnt work and gives me an error.
I can replicate this error when i open a terminal as root user. I logged in using su and typed my password. Typing in the command then gives me an error and it does not execute. The same when i try to execute the command with python using either the modules os or subprocess.
Is it because my command to change the wallpaper which starts with pcmanfm (which is the window manager) has probblems with its path and when i am root user the path is changed?
How can i make python open a terminal as "normal" user in my case "pi" on my raspberry pi, and run a command in it?
this is the terminal command which changes my desktop wallpaper:
pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg
this is what happens when i run my python code to open a terminal:
root#raspberrypi:/home/pi/Desktop#
instead of normally when it works:
pi#raspberrypi:~ $
here is my python code which is meant to open a new terminal with the command which changes my desktop wallpaper, however i end up as ROOT user as described above and get an error and nothing happens:
import os,random
import subprocess as sub
sub.call('lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"', shell=True)
executing the following from a normal terminal works perfectly:
lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"
this is the result after running the python code, which is the error i always get:
** Message: 12:11:08.734: x-terminal-emulator has very limited support, consider choose another terminal
root#raspberrypi:/home/pi/Desktop#
So how can i make python open a terminal as user "pi" not as root?
or is this not the problem?
thanks!
The problem was calling the python script as sudo.
Opening the python script normally as:
Python3 script.py
Will open a terminal as a normal user and. change my desktop wallpaper,
Whereas sudo python3 script.py opens the terminal as root user and then the command to change wallpaper no longer works.
I have a Python script which is running a shell script using the subprocess library. It has to run on any platform so I have 2 shell scripts, one for Linux/MacOS (cm) and one for Windows (cm.cmd).
Let's say they both contain just a single command example_command -param.
The code which is running the shell script looks like the following:
json = subprocess.run(['cm'], shell=True)
This way, thanks to the shell handling the execution of the script (shell=True), it runs the script cm on Linux/MacOS platforms and cm.cmd on Windows.
The output of the script is a JSON and it works properly on Linux/MacOS platforms, the only problem is with Windows where the output contains the shell prompt which breaks the JSON obviously.
The captured output in the json variable may look like this:
My prompt c:\ $ example_command -param
{ "json_data": ... }
How to avoid printing of the prompt to the subprocess output?
It's caused by the feature called command echoing which is enabled by default but it may be disabled using the echo command. From the documentation:
Syntax
echo [on | off]
Parameters
[on | off] Turns on or off the command echoing feature. Command echoing is on by default.
If you add echo off at the first line of the script, it will disable the command echoing for all subsequent commands but it will echo the echo off command itself. To suppress even echoing of that command, simply prefix it with #.
At sign (#) as a command prefix has the same effect as echo off but only for a single command.
So to summarize it: Simply add #echo off at the first line of the shell script (or batch in Windows terminology) and that's it. Only the output of command(s) executed in the script will be sent to stdout.
I am trying to run a python script with chef, and the script will exit when chef exits because of SIGHUP. I am using nohup but it still gets the signal. Any ideas how I can get this script to run in the background?
nohup python simple-setup.py --dbpath /media/ephemeral0/mongo-data/ -n 2 --name dev --arbiters 1 --mongo_path /usr/bin/ > /media/ephemeral0/log/set.log 2>&1 &
I got round a problem like this by putting the command in a shell script, with an & on the end and then nohupping the shell script. If it helps.