Shell script kill command - python

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")

Related

Cronjob on Ubuntu Mate for Raspberry stops right after execution

I use a RaspberryPi 3 with UbuntuMate 16.04. On it, I want to start a little Python (3.5) program every midnight. For that I call a little shell script, so that I can change into the wanted directory comfortably.
crontab:
5 0 * * * /path/to/script/start.sh
start.sh (yes, it's executable):
#!/bin/bash
cd /path/to/wanted/workingDir/
python3.5 ControllerQueue.py
#also tried: python3.5 ControllerQueue.py &
Now if I execute the programm or script from the terminal, everything runs fine. But if I use the crontab it starts the script and stops right after. I also tried running the programm directly but get the same outcome. The paths are correct as I copied the workingDir path from the crontab-file and started it via the terminal.
Is there something I overlook?
As stofvl suggested, I saved the error output of my shell script. It turns out that I needed to add a display. My programm is divided into two scripts. One which provides a GUI and the other main application. The script only starts the main application, without the GUI, but it seems that this didn't matter.
This discussion helped me solve the problem.

run os.system commands on a new terminal- Python 3

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

Open terminal, run python script and keep it open for results?

How to get an sh script for starting a new terminal, execute a python script and keep it running? The python script is supposed to run continuously in a perpetual loop, spitting out results as they pop in. Whenever trying with sh-script for gnome-terminal just getting: child process exited normally with status 2
Manually it would just be: python home/ubuntu/pyscript.py
Could someone give an idea how to do this?
I have a list of scripts to run, so resorting to the manual solution is tedious.
You can use gnome-terminal with the -x flag.
Suppose you have a spam.py script; then the following command will spawn a new terminal, run spam.py in it, and close the terminal once the script has ended.
gnome-terminal -x python spam.py
Try with this script:
# spam.py
import time
for _ in range(5):
print("eggs")
time.sleep(1)
Then the previous command will spawn a terminal, that will be printed eggs five times, and then will be closed.
If you want to leave the terminal open with the Python interpret still running after the script ended, then Python's -i flag (doc then CTRL+F -> -i) is what you want:
gnome-terminal -x python -i spam.py
To run the Python script in a new instance of your favourite terminal, write:
x-terminal-emulator -e python -i home/ubuntu/pyscript.py
This will start the Python script and run it until it ends, then display a Python prompt to stop the terminal emulator from closing.
This will work with x-terminal-emulator substituted with any of the many, many terminals installed on my computer, so will work with little modification across all POSIX-compatible systems with the standard terminals installed. This won't work on a Mac, however. For a properly cross-platform Python implementation of something slightly different, see here. Most of the techniques should be transferable.
To run the Python script in the same terminal whilst carrying on with the rest of the shell script, write:
python home/ubuntu/pyscript.py &
Note the &, which runs the program as a new process (but still connects the output to the virtual terminal).

Raspberry pi crontab and interrupts in python

I wrote a python script that logs some data to a txt file with the absolute path /home/pi/foo.txt through button presses that trigger interrupts. I've started the script many times through the command line without any problems: when you press go, it goes, and when you press stop, it stops. The script is located in /home/pi/log.py. I wrote a shell script that will execute this python script because I read that it may help on a tutorial, so let's call it log.sh which calls
#! /bin/sh
cd /home/pi
/usr/bin/python /home/pi/log.py
However, when I attempt to start this script through crontab by adding it to my
#reboot log.sh
the script will run, but no button presses will stop the script (aka stop button won't work). The cpu usage goes up to 100% and sticks there. I've tried copying and pasting the environment variables from my user environment into crontab, but that won't work either.
Any ideas?
I learned the hard way that the environment that crontab uses isn't the same as your user's environment.
Check out this stack over flow for more info about environment difference between user and crontab: https://serverfault.com/questions/698577/why-is-the-cron-env-different-from-the-users-env/698635
The best thing in my opinion to do is to just do everything in python. There's a python environment that behaves like crontab. It's called schedule(https://pypi.python.org/pypi/schedule).
I'm actually using it in a raspberry Pi thermostat project.
check it out here:https://github.com/mababio/raspi_thermostat/blob/c6aea6ded6874d0dc21ded34f07874dd7f97dd15/src/thermo/test/jobs.py

Waiting until a os.system command finishes in my virtual machine (Windows 7)

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!

Categories