Use Raspberry Pi 4B Terminal Command in Python Application - python

I am currently working on a small Python Application which turns a Raspberry Pi 4B into a 48 Channel Audio-Recorder. Basics work, but during Recording, I need a log file which tells me when recording started, which ALSA warnings occurred and when recording stopped.
The recorder can be started with this terminal command:
pi#raspberrypi:~ $ rec -q -t caf --endian little --buffer 96000 -c 48 -b 24 /home/pi/myssd-one/Aufnahmen/test.caf 2>&1 | tee /home/pi/myssd-one/Aufnahmen/logging.log
this records audio in the test.caf file and writes ALSA warnings to logging.log
So far so good.
The Python Program (which should run on a touchscreen with GUI so recording can easily started and stopped) takes care of variable audio-filenames (date-time-stamp) and controls an LED to show that recording is running.
This part of the code takes care of switching on and off:
#!/usr/bin/env python
from tkinter import *
import shlex
import os
import subprocess
import tkinter.font
import datetime
from gpiozero import LED
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.OUT)
def ledToggle():
if led.is_lit:
led.off()
my_env = os.environ.copy()
my_env['AUDIODRIVER'] = 'alsa'
my_env['AUDIODEV'] = 'hw:KTUSB,0'
ledButton["text"] = "Turn Recorder on"
print ("recorder stops")
subprocess.Popen(['sudo', 'pkill', '-SIGINT', 'rec'], env = my_env, shell = FALSE, stdout=subprocess.PIPE)
else:
led.on()
my_env = os.environ.copy()
my_env['AUDIODRIVER'] = 'alsa'
my_env['AUDIODEV'] = 'hw:KTUSB,0'
ledButton["text"] = "Turn Recorder off"
print ("recorder starts")
##reference statement command line: "rec -q -t caf --endian little --buffer 96000 -c 48 -b 24 /home/pi/myssd-one/Aufnahmen/test.caf 2>&1 | tee /home/pi/myssd-one/Aufnahmen/logging.log"
command_line = shlex.split("rec '-q' '-t' 'caf' '--buffer' '96000' '-c 48' '-b 24' '/home/pi/myssd-one/Aufnahmen/test.caf' '"2>&1 | tee"' '/home/pi/myssd-one/Aufnahmen/logging.log'")
p1 = subprocess.Popen(command_line, env = my_env, shell = False, stdout=subprocess.PIPE)
I am trying to move the original command line statement into the subprocess.Popen command, to no success yet. The part where routing to the log file is done, fails. It looks as the initiating sox-application 'rec' tries to interpret it as part of its own parameter list, instead of interpreting it as a redirection of stdout and stderr to the log file. I appreciate some guidance in this issue.
Variable Filenames for audio files is already done, but for simplicity taken out of this code snippet.

Thanks Mark, I dived into this command line along your hint that it only can run with shell=True and this implied that it had to be written as a full statement without separating commas and escape quotes. Now it works. Actually, the shlex.split() becomes obsolete.

Related

Terminate Linux process using Python - ffmpeg to terminate a live stream to Youtube

I'm using Raspberry Pi to live stream to Youtube. I use Python and this is my script.
import time
import os
key = 'YouTube'
loader='ffmpeg -f pulse -1 alsa output.platform-bcm2835 audio.analog-stereo.monitor f xllgrab -framerate 24 -video size 740x480-B etc....' + key)
proc= os.system(loader)
process_id = proc.pid
print('Running'.process_id)
time.sleep (60)
proc.terminate()
import sys
sys.exit("Error message")
The script works. However, when I try and terminate it, it remains streaming even though the script finishes.
I also tried terminating the script process_id. However it doesn't stop the stream.
I use Python subprocess and that has never caused a problem in Linux (well, limited to pytest on GitHub Action to be honest):
import time
import subprocess as sp
import shlex
key = 'YouTube'
# this doesn't look like a valid FFmpeg command. Use the version which
# successfully streamed data
loader='ffmpeg -f pulse -1 alsa output.platform-bcm2835 audio.analog-stereo.monitor f xllgrab -framerate 24 -video size 740x480-B etc....' + key)
proc= sp.Popen(shlex.split(loader))
print(f'Running{proc.pid}')
time.sleep (60)
proc.terminate() # or proc.kill()
import sys
sys.exit("Error message")
If it is pulse or x11grab device refusing to terminate, you may need to 'kill' it instead.

I cannot list up the BLE devices in the neighbourhood of my Raspberry Pi (python, btmgmt)

I want to scan the ble devices in the environment of my Raspberry, by using a python script called from a cron script.
But when I do this in cron (I mean I added to sudo crontab -e), I allways end up with an empty list.
when I am logged in as pi user - btmgmt works (only) fine with su permissions:
pi#Pluto:~ $ btmgmt find
Unable to start discovery. status 0x14 (Permission Denied)
pi#Pluto:~ $ sudo btmgmt find
Discovery started
hci0 type 7 discovering on
hci0 dev_found: 77:F8:D7:8A:1E:E5 type LE Random rssi -83 flags 0x0000
...
so in my python script I wrote:
flog.write("P01:\r\n")
out = subprocess.Popen(['sudo', '/usr/bin/btmgmt', 'find'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = out.communicate()
flog.write("stderr: " + str(stderr) + "\r\n")
cDvc = stdout.split('\n')
flog.write("Amount of lines = " + str(len(cDvc)) + "\r\n")
for line in cDvc:
line = line + '\r\n'
if debugflag:
print(line)
flog.write(line)
..
Running this script from the shell prompt works fine.. in the log-file (flog) I get: ...
P01:
stderr: None
Amount of lines = 40
Discovery started
hci0 type 7 discovering on
hci0 dev_found: 70:D0:FD:74:34:AC type LE Random rssi -59 flags 0x0000
AD flags 0x1a
..
Running this same script as a crontab -e line: no devices show up & I cannot find cause:
...
P01:
stderr: None
Amount of lines = 1
P02:
...
Can anyone help me out here?
If you use the BlueZ DBus API to get the information then you will not need to use sudo. It also avoids you having to use btmgmt as I am not sure it was intended for it to be scripted in that way
The documentation for the DBus API is available at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
The pydbus library is very helpful for accessing the BlueZ DBus API: https://pypi.org/project/pydbus/
Some useful things to know to get you started:
The Dbus service for bluez is called 'org.bluez'
The default Bluetooth adapter normally has '/org/bluez/hci0' as its DBus object path.
BlueZ/DBus has an object manager which stores information about devices
I did the following script to test the idea:
from datetime import datetime
import os
import pydbus
from gi.repository import GLib
discovery_time = 60
log_file = '/home/pi/device.log'
# Create an empty log file
def write_to_log(address, rssi):
if os.path.exists(log_file):
open_mode = 'a'
else:
open_mode = 'w'
with open(log_file, open_mode) as dev_log:
now = datetime.now()
current_time = now.strftime('%H:%M:%S')
dev_log.write(f'Device seen[{current_time}]: {address} # {rssi} dBm\n')
bus = pydbus.SystemBus()
mainloop = GLib.MainLoop()
class DeviceMonitor:
def __init__(self, path_obj):
self.device = bus.get('org.bluez', path_obj)
self.device.onPropertiesChanged = self.prop_changed
print(f'Device added to monitor {self.device.Address}')
def prop_changed(self, iface, props_changed, props_removed):
rssi = props_changed.get('RSSI', None)
if rssi is not None:
print(f'\tDevice Seen: {self.device.Address} # {rssi} dBm')
write_to_log(self.device.Address, rssi)
def end_discovery():
"""Handler for end of discovery"""
mainloop.quit()
adapter.StopDiscovery()
def new_iface(path, iface_props):
"""If a new dbus interfaces is a device, add it to be monitored"""
device_addr = iface_props.get('org.bluez.Device1', {}).get('Address')
if device_addr:
DeviceMonitor(path)
# BlueZ object manager
mngr = bus.get('org.bluez', '/')
mngr.onInterfacesAdded = new_iface
# Connect to the DBus api for the Bluetooth adapter
adapter = bus.get('org.bluez', '/org/bluez/hci0')
adapter.DuplicateData = False
# Iterate around already known devices and add to monitor
mng_objs = mngr.GetManagedObjects()
for path in mng_objs:
device = mng_objs[path].get('org.bluez.Device1', {}).get('Address', [])
if device:
DeviceMonitor(path)
# Run discovery for discovery_time
adapter.StartDiscovery()
GLib.timeout_add_seconds(discovery_time, end_discovery)
print('Finding nearby devices...')
try:
mainloop.run()
except KeyboardInterrupt:
end_discovery()
I have exact the same issue. I need to use a raspberry pi to check if some specific bluetooth devices are alive nearby and to send a heartbeat to monitoring service.
I was not getting any output from sudo btmgmt find when the command was executed in a cron job like * * * * * sudo btmgmt find > /tmp/ble_devices.txt, neither if I was using python to capture the output from Popen call. So I asked myself if I could executed it into another screen, and it worked.
My solution is quite hackish. I did the following:
Installed the screen tool on raspberry pi: sudo apt install screen
Created a screen for running the scan command: screen -S blescan
Detached myself from screen ctrl+a+d
Created a shell script in /home/pi/scan_job with the content:
#!/bin/bash
cd <to python project> && ./<file to be executed>
Made it executable chmod +x /home/pi/scan_job
Set cronjob to execute the file in blescan screen:
*/10 * * * * screen -S blescan -X screen '/home/pi/scan_job'

Bad Display Name when running Python script on boot with Touch Screen

Attempting to run a Python script on boot on Raspberry Pi 3B+ 1GB RAM, Raspbian, with a SunFounder 10" Touch Screen, - .log file returns "Bad display name'
Python script is 100% functional when run via Terminal / executable script / Thonny etc. Attempted to run at boot first via rc.local - created a service, enabled service, daemon-reload... etc. Did not work.
Tried to run as crontab, same result - .log output from crontab shows "Bad display name". Thought it was lack of Display Environment imported and declared within the Python script, so I added that - but on boot returns the same result.
This is the Python Script I'm using
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import os
import sys
import webbrowser
import time
import subprocess
from pynput import keyboard
from Xlib.display import Display
#GPIO Readout
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#GPIO Header Setup
header = 2
GPIO.setup(header, GPIO.IN)
#Omxplayer Commands
Loop = 'omxplayer -b --loop --no-osd -o hdmi /home/pi/Videos/PlanetEarth.mp4 > /dev/null'
Donation = 'omxplayer -b --no-osd -o hdmi /home/pi/Videos/Cartoon.mp4 > /dev/null'
KillPlayer = 'pkill omxplayer.bin'
KillForm = 'pkill chromium'
#Set Display Environment
new_env = dict(os.environ)
new_env['DISPLAY'] = ':0.0'
#Form Handling Required Below
#If Donation is successful, Stop Looping Video, Open Form in Chromium, Wait 60 seconds, Close Chromium, Restart Loop
def PullDownSuccess():
subprocess.Popen(KillPlayer, env=new_env, shell=True)
time.sleep(2)
webbrowser.open('<url>')
time.sleep(60)
subprocess.Popen(KillForm, env=new_env, shell=True)
time.sleep(2)
subprocess.Popen(Loop, env=new_env, shell=True)
#Inception
subprocess.Popen(Loop, env=new_env, shell=True)
#Terminate Loop with Escape Key or Manually Initiate Donation Success
def on_press(key):
if key == keyboard.Key.ctrl:
PullDownSuccess()
if key == keyboard.Key.esc:
subprocess.Popen(KillPlayer, shell=True)
#Keyboard Listener Module
with keyboard.Listener(
on_press=on_press) as listener:
listener.join()
#Donation Successful Do:
while True:
header_state = GPIO.input(header)
if header_state == GPIO.HIGH:
PullDownSuccess()
I am currently attempting to run this script on Boot via crontab with this line:
#reboot (/bin/sleep 10; /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py > /home/pi/Custom_Scripts/cronjoblog 2>&1)
The error log file for crontab returns the following:
raise error.DisplayNameError(display)
Xlib.error.DisplayNameError: Bad display name ""
This error only exists when attempting to run script on boot up. Is the Display overriding the boot display permissions on Boot Up? What is keeping the script from running on the Display on boot up, but not when remotely executed? Thank you for your consideration.
Update: Still no solution. Display environment returns ":0.0' ... so far I have tried to remove
> /dev/null from #Omxplayer Commands
Replacing crontab startup line to:
DISPLAY=":0" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py
and
DISPLAY=":0.0" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py
And any possible combination of these.
Confirmed the script is not waiting for any background processes as I have added delay (time.sleep) up to 30 seconds, as well as returning IP addresses, etc.
Returns either Bad Display Name still OR "Can't connect to display ":0": b'Invalid MIT-MAGIC-COOKIE-1 key"
Still looking for a solution if anyone has one.
EDIT:
Fixed using /LXDE-pi/autostart. Answer below.
Try to add DISPLAY environment variable before calling your script:
DISPLAY=":0" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py
DISPLAY="/dev/null" /usr/bin/python3 /home/pi/Custom_Scripts/<script>.py
Have you tried a longer sleep?
If you try to get your ip adress during your startup script with (for example) :
import socket
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
you will get an errror like:
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 101] Network is unreachable
This is because at reboot network is not necessarily available. In order to make it available
see https://raspberrypi.stackexchange.com/questions/45769/how-to-wait-for-networking-on-login-after-reboot :
sudo raspi-config
then select option 3 Boot Options
then select option 4 Wait for network at boot
Once you have done that networking should not be an issue at reboot.
You get also check for other ways to enforce network setup before running your script take a look at :
https://askubuntu.com/questions/3299/how-to-run-cron-job-when-network-is-up
If there are unforeseen ramifications for using this method, I will update this thread accordingly. I fixed this issue just by adding two lines to the autostart file which resides in /etc/sdg/lxsession/LXDE-pi/autostart.
sudo nano /etc/xgd/lxsession/LXDE-pi/autostart
Add these two lines, do not modify the existing code.
sleep 5
#/usr/bin/python3 /home/pi/Custom_Scripts/<script>.py
My autostart file looks like this:
#lxpanel --profile LXDE-pi
#pcmanfm --desktop --profile LXDE-pi
#xscreensaver -no-splash
point-rpi
sleep 5
#usr/bin/python3 /home/pi/Custom_Scripts/<script>/py
Ctrl + O to write file
sudo reboot

Capture and save running process

I want to capture "print('Recording GPS Position...')" on the SD card. For now, this is printing on terminal directly I want to capture this runtime process (p_1) from terminal and store on the SD card as it is being executed. How do I do this?
Also in general what is a way to capture and store processes at runtime from terminal and store on the SD card (note I do want to store processes as it is being executed and not after its execution is done).
import sys
import os
import time
import subprocess, shlex
import signal
import serial
import psutil
from subprocess import Popen, PIPE
def recording():
flag = 0
ser = serial.Serial('/dev/ttyACM0', 921600, timeout=1)
ser.flushOutput()
# ROSBAG Recordings (Shell commands that execute the messages on the terminal)
messages = 'rosbag record -o GPS_Position.bag dji_sdk/gps_position', 'rosbag record -o IMU_Data.bag dji_sdk/imu', 'rosbag record -o Attitude.bag dji_sdk/attitude', 'rosbag record -o Velodyne_Packets.bag velodyne_packets', 'rosbag record -o Velodyne_Points.bag velodyne_points', # rosbag record -o Velocity.bag dji_sdk/velocity'
while flag == 0:
try:
args1 = shlex.split(messages[0]) # messages[0] = rosbag record -o GPS_Position.bag dji_sdk/gps_position
#print (args1)
p_1 = subprocess.Popen(args1, stdout=PIPE)
print('Recording GPS Position...')
p_1.stdout.flush()
You'll want to choose a directory for your SD card, and instead of print()ing to the terminal you'll write() to the file you choose.
Here is the documentation: https://docs.python.org/3/tutorial/inputoutput.html
Edit: it's been answered better here https://stackoverflow.com/a/8024254/8240691 but it would still be worth your while to look over the input/output documentation.

Python script to run background services over SSH

Summary: I am ssh'ing to a remote server and executing a fork1.py script over there which is shown below. But the trouble is that I want the processes to execute in the background, so that I can start multiple services.
I know we can use nohup, etc. but they are not working. Even when I use a & at the end, the process starts, but gets killed when the script terminates.
Here is the code:
import os
import sys
import commands
from var import key_loc
import subprocess
import pipes
import shlex
def check(status):
if status != 0:
print 'Error! '
quit()
else:
print 'Success :) '
file1=open('/home/modiuser/status.txt','a')
file1.write("Success :)\n")
if(sys.argv[1]=="ES"):
os.chdir('/home/modiuser/elasticsearch-0.90.0/bin/')
proc1=subprocess.Popen(shlex.split("nohup ./elasticsearch -p /home/modiuser/es.pid"))
if(sys.argv[1]=="REDIS"):
os.chdir('/home/modiuser/redis-2.6.13/src')
proc2=subprocess.Popen(shlex.split("./redis_ss -p /home/modiuser/redis.pid"))
if(sys.argv[1]=="PARSER"):
proc3=subprocess.Popen(shlex.split("nohup java -jar logstash-1.1.12-flatjar.jar agent -f parser.conf"))
file1=open('/home/modiuser/pid.txt','a')
file1.write("PARSER-"+str(proc3.pid)+"\n")
file1.write(str(proc3.poll()))
file1.close()
if(sys.argv[1]=="SHIPPER_TCP"):
proc4=subprocess.Popen(shlex.split("nohup java -jar logstash-1.1.12-flatjar.jar agent -f shipper_TCP.conf"))
file1=open('/home/modiuser/pid.txt','a')
file1.write("SHIPPER_TCP-"+str(proc4.pid)+"\n")
file1.close()
Where am I going wrong?
just try with
import os
os.system('python program1.py &') #this one runs in the background
os.system('python program2.py') #this one runs in the foreground

Categories

Resources