I am facing a wired bug in my Raspberry Pi where my startup Python script can't play sound. I trigger my script to run at startup with ~/.bashrc file. Here is the code in ~/.bashrc.
# run python script
openvt -s -w /home/pi/path/to/script.sh
where script.sh will run python script. The code in script.sh is as simple as cd to python script and run it.
cd /path/to/project/folder
python script.py
Everything works great except the audio.
Here is the code in my Python script which will play the sound
import subprocess
subprocess.Popen(['omxplayer', "/path/to/audio.wav".format(filename)], stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
Note: I've tried running python script manually without .bashrc it works fine, there is no problem with audio. I also tried running python script in /etc/rc.local, audio works great. It seems like all script triggered from .bashrc can't play audio.
The reason why I don't run script from /etc/rc.local is that it runs as root user and has problem with my Ajax request, ~/.bashrc doesn't have problem with my Ajax.
How do I play sound in startup script? Thanks in advance for any helps
Related
I already found a lot of posts with crontab errors but i still can't figure out where is my issu.
I want to start a python script on /home/pi/somedir/main.py and to fix any relative path issu in my script i made a start.sh next to it containing
cd /home/pi/somedir
/usr/bin/python3.7 main.py
I made it executable with sudo chmod -x start.sh and it works find went i do /home/pi/somedir/start.sh manually, but i want to autostart it at start.
I tried multiple ways (like /etc/rc.local) and it never worked. As crontab seems to be the better way to do this, i tried crontab -e (the crontab for the pi user so) and i added
#reboot bash /home/pi/somedir/start.sh > /home/pi/somedir/logstart.txt &
to have the logs and to execute the script without blocking the rest of the system. But the script doesn't start and there is nothing in the log file.
The script is a python3 discord bot with logging output, running just find when launched manually. It is a raspberry pi 3B+, with a fresh install of raspberry pi os desktop and i have nothing else running on it.
Thanks for any help and sorry if my english is not correct !
ps: If you know better options to host python3 scripts like discord bots on raspberry pi it would also be usefull of course, i am clearly a beginner !
The issu was that the script won't start at reboot as it is before the system is fully booted. One solution was to start the process at precise hour when the system is fully start, like every morning. An other would be to add a delay before running the start.sh script, somethings like
#reboot sleep 60 && /home/pi/somedir/start.sh > /home/pi/somedir/logstart.txt &
works for me aswell.
In crontab I have:
#reboot /usr/bin/python3 /root/mydirectory/mypythonfile.py
Using: which python3 I get: /usr/bin/python3
at the start of the python script I have: #!/usr/bin/python3
The python file is executable for everyone.
Running ./mypythonfile.py in shell works o.k.
I have no idea left what to do, to make this file execute via crontab. I played in crontab and executed some commands (not python script) and it worked fine.
I am logged in as Root, so this should not be a problem as well.
EDIT:
How do I know it's not running?
I created a script, which sends e-mail, inserts dummy data in database and I checked Htop for .py file. Zero on all.
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 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
I want to run a python script at boot of Lubuntu 15.04. This python script writes some string into a text file placed in /home/aUser/aFile.txt
My /etc/rc.local file is:
#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
And the script /home/aUser/theScript.py is:
#!/usr/bin/python
f = open('/home/aUser/aFile.txt','w');
f.write("Some string...");
f.close();
Actually the python script does more, and run an infinite loop, this is why I run the script in background with &. Of course I have python installed:
~$ python --version
Python 2.7.9
I checked if /etc/rc.local is called at boot, and it is, proof of that: I added a test into the /etc/rc.local in this way:
#!/bin/sh -e
python /home/aUser/theScript.py &
exit 0
echo "Test" >> /home/aUser/aTest.txt
and the file /home/aUser/aTest.txt is written/created at boot.
So everything looks correct, proof of that:
if I run manually ~$ /etc/rc.local the file aFile.txt is correctly written.
Instead if I start (or reboot) the OS, the file is not written at boot.
I suspect that could be a problem of permissions/user: I know that /etc/rc.local is run as root, but even if I set root or aUser as owner of the file, the situation is the same. Also run the python script in the /etc/rc.local as user aUser (with su command) does not solve the problem.
Ok I found the problem and fix it, thanks to the #Zac comment.
Actually the python script try to open a network connection before writing the file: at boot time, when the python script is run from /etc/rc.local (so, it is run), the network is still not ready (probably because it is a wireless network) and therefore an exception is raised and the entire script stops. Capturing the exception solves the problem.
So at the end it was my fault, (not) helped by the rc.local that does not provide an easy way to debug.