The CGI python script shown below, sound.py, plays a sound through a speaker connected to a Raspberry’s audio output. The sound plays fine when I execute the script from the command line via “python sound.py”. However, when I try to trigger it as a cgi script by invoking it via the Web (http://192.168.1.246/cgi-bin/sound.py), the sound won’t play, and the browser will only display “Hi Hello World!” (which, I suppose, indicates that the server at least recognizes it as a CGI script and executes it without producing errors).
I thought that the issue might have to do with the permissions/ownership of the sound file and tinkered around with those, but that didn’t help (I’m also pasting below information about the ownership and permission of the script itself, and the sound file).
Thanks a lot for helping!
Marc.
-rwxrwxrwx 1 root root 441 Jul 10 23:23 sound.py
-rwxr-xr-x 1 root root 29812 Jul 9 22:58 cardinal-short.mp3
Script (sound.py):
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import pygame
name = "cardinal-short"
pygame.mixer.init()
pygame.mixer.music.load("/var/www/html/cgi-bin/cardinal-short.mp3")
pygame.mixer.music.play()
pygame.mixer.music.set_volume(1.0)
while (pygame.mixer.music.get_busy() == True):
continue
pygame.mixer.quit()
print "Content-Type: text/plain;charset=utf-8"
print
print "Hi Hello World!"
I was able to fix this via:
sudo usermod -aG audio www-data
and then restarting apache.
The problem was that, and I quote from elsewhere on the web:
the sound devices (in /dev/snd/) are only accessible to members of group "audio". pi is a member of that group, and www-data is not.
Related
I have successfully gotten a shell script to run whenever a particular usb is plugged into my RPi using the following in /etc/udev/rules.d/test.rules
ACTION=="add", ATTRS{idVendor}=="****", ATTRS{idProduct}=="214f", RUN+="/home/pi/script.sh"
In /home/pi/script/sh is
#!/bin/bash
echo "Hey!" >> /home/pi/hey.txt
/home/pi/python_script.py
This does successfully create a file in /home/pi called hey.txt with "Hey!" in it. However, it seems that the execution of this shell script fails trying to run /home/pi/python_script.py. I have made it executable with chmod +x /home/pi/python_script.py and am even able to run the shell script from terminal and it will work perfectly fine. The first line of my python script is #!/usr/bin/python3 which is the location of my python3 executable when I do which python3. I have also tried to have the line that runs the python3 script be different things like python3 /home/pi/python_script.py and /usr/bin/python3 /home/pi/python_script.py and /usr/bin/env python3 /home/pi/python_script.py to no avail.
The error I get when I do tail -f /var/log/syslog and plug in the USB is
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Aug 31 10:56:31 raspberrypi systemd-udevd[14417]: Process '/home/pi/script.sh' failed with exit code 1.
Also, the python3 script has code to create a file in the directory as well as write to the log so that I may verify it is running. Here is the beginning of the python3 script.
#!/usr/bin/python3
import serial
import subprocess
from time import sleep, time
import logging
from systemd.journal import JournaldLogHandler
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(module)s - %(funcName)s: %(message)s")
logging.info("Starting script!")
log = open("/home/pi/log.txt", "w")
In summary, I am able to run the script from terminal successfully and able to run the python3 script as both an executable and using python3. I am also able to see that the script does execute the first task it has when the usb is plugged in, but fails seemingly at the python3 portion.
Is there something wrong with the way I am trying to run python? I have written systemd services that successfully run python scripts, so I am confused.
EDIT:
Thanks to #KlausD I see that the issue can be elaborated to that when I run it from terminal, I get no errors, but when it runs automatically from udev, I get an import error for certain modules like logging and serial, but not time.
Here is the error:
ModuleNotFoundError: No module named 'serial`
It looks like the issue was that the python3 interpreter didn't know where to look for modules, so the answer here fixed my issue.
https://askubuntu.com/questions/1406907/python-script-called-by-udev-rule-unable-to-import-modules-modulenotfound
I put the following code at the beginning of my script and swapped out the below path for my python3 version's path and it runs!
import os
import sys
sys.path.append(os.path.abspath("/usr/local/lib/python3.9/dist-packages"))
I have an issue with cron job on a Raspberry Pi.
I've search the internet and StackOverflow but nothing seems to fix my problem.
I'm new to Linux and think I might have broken something that I don't understant yet.
So : Cron tasks work but not fully.
I have a test script called test2.py writing in a file and printing File created in the console.
When I run this script myself, it works.
When I run this script with cron, it prints the "File created" but won't create the file or write in it.
I watched syslog, cron is running the task and logging in a file. This log file only give me the print output and no other error. File was not created.
Where did I mess this up ?
TLDR : Cron run the task, I can see the script print but not writing in a file.
test2.py :
file = open('testfile.txt','w')
file.write('Hello World')
file.close()
print('File created')
This is the Crontab entry : */1 * * * * /usr/bin/python3 /var/www/test2.py >> /tmp/cron.log 2>&1
Here are the perm on the files :
273281 4.0K -rw-r--r-- 1 pi pi 211 Dec 17 11:32 test2.py
Or, not working either :
273281 4.0K -rwxrwxrwx 1 pi pi 211 Dec 17 11:32 test2.py
Thanks for your help and suggestions.
Tin Nguyen solved this for me.
It was simply a path issue. File is created somewhere. My path was relative and cron directory is not the same as the one I ran the script from.
Thanks a lot.
Thank you for reading. I have this pi tft screen with tactile buttons connected to it. When a button is held for two seconds, I´d like to open a terminal and run a bash script. The problem is that I need to view the output/progress of the script in realtime.
This python script, which monitors GPIO, works perfectly if a terminal is already open, then I can see the output.
from gpiozero import Button
from signal import pause
import subprocess
def runboth():
subprocess.call(["/opt/script/test.sh"])
runboth_btn = Button(17, hold_time=2)
runboth_btn.when_held = runboth
pause()
But if the pythons script is run from boot for example, no terminal will open since I guess it is running in the background? How can I achieve that the script will also open a terminal ?
I have tried the following but without luck.
from gpiozero import Button
from signal import pause
import subprocess
def runboth():
subprocess.call(["lxterminal", "--command=/opt/script/test.sh"])
runboth_btn = Button(17, hold_time=2)
runboth_btn.when_held = runboth
pause()
I´m met with this error
root : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/usr/bin/lxterminal --command=/opt/script/test.sh
Mar 24 23:24:42 raspberrypi sudo[1315]: pam_unix(sudo:session): session opened for user root by (uid=0)
Mar 24 23:24:42 raspberrypi lxterminal[1319]: cannot open display: 0:0
Or if run from terminal
lxterminal:1404): Gtk-WARNING **: cannot open display: :0.0
This is the bash scripts I am currently using for test.
#!/bin/bash
while true; do
echo "hello"
done
I run a Raspberry PI 3, Raspian OS with root privileges.
Again, thank you for reading and taking your time!
I'm using a Raspberry Pi to run a python script that both plays a sound (using pygame) and uses the onboard GPIO. I know to access the GPIO the script must be ran using sudo, but I've noticed that using sudo python myscript.py the code executes, but does not play the sound. Using python myscript.py the sound plays, but obviously the not GPIO. I've tried changing where I am referencing the audio file to an absolute path to no avail, I'm not overly familiar with inner workings of sudo, can anyone shed some light on this problem?
import pygame
pygame.init()
pygame.mixer.music.load("alert.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
continue
GPIO.output(16,True)
GPIO.output(18,True)
Output of printenv
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
SSH_CONNECTION=192.168.0.114 64152 192.168.0.196 22
LANG=en_US.UTF-8
INFINALITY_FT_CHROMEOS_STYLE_SHARPENING_STRENGTH=0
INFINALITY_FT_AUTOHINT_INCREASE_GLYPH_HEIGHTS=true
INFINALITY_FT_CONTRAST=0
INFINALITY_FT_STEM_FITTING_STRENGTH=25
INFINALITY_FT_GLOBAL_EMBOLDEN_X_VALUE=0
XDG_SESSION_ID=c5
USER=pi
INFINALITY_FT_AUTOHINT_SNAP_STEM_HEIGHT=100
INFINALITY_FT_GRAYSCALE_FILTER_STRENGTH=0
INFINALITY_FT_GAMMA_CORRECTION=0 100
PWD=/home/pi
HOME=/home/pi
TEXTDOMAIN=Linux-PAM
SSH_CLIENT=192.168.0.114 64152 22
INFINALITY_FT_WINDOWS_STYLE_SHARPENING_STRENGTH=10
SSH_TTY=/dev/pts/1
INFINALITY_FT_BRIGHTNESS=0
MAIL=/var/mail/pi
TERM=xterm
SHELL=/bin/bash
INFINALITY_FT_USE_VARIOUS_TWEAKS=true
SHLVL=1
INFINALITY_FT_BOLD_EMBOLDEN_Y_VALUE=0
INFINALITY_FT_GLOBAL_EMBOLDEN_Y_VALUE=0
INFINALITY_FT_AUTOHINT_HORIZONTAL_STEM_DARKEN_STRENGTH=10
LOGNAME=pi
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
INFINALITY_FT_FILTER_PARAMS=11 22 38 22 11
INFINALITY_FT_USE_KNOWN_SETTINGS_ON_SELECTED_FONTS=true
INFINALITY_FT_STEM_SNAPPING_SLIDING_SCALE=40
INFINALITY_FT_STEM_ALIGNMENT_STRENGTH=25
INFINALITY_FT_BOLD_EMBOLDEN_X_VALUE=0
INFINALITY_FT_AUTOHINT_VERTICAL_STEM_DARKEN_STRENGTH=25
INFINALITY_FT_FRINGE_FILTER_STRENGTH=0
_=/usr/bin/printenv
Output of sudo printenv
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
LANG=en_US.UTF-8
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/root
LOGNAME=root
USER=root
USERNAME=root
HOME=/root
SHELL=/bin/bash
SUDO_COMMAND=/usr/bin/printenv
SUDO_USER=pi
SUDO_UID=1000
SUDO_GID=1000
Another solution that doesn't require sudo, would be to create and use a gpio group. On raspbian, you can create a new group with groupadd.
Assign that group to the gpio devices with group read/write access for that group. You can do that with chown, but that won't last after a reboot.
On raspbian this is done with permanently a udev rule. See e.g. this example.
Then add the user-id that runs the Python programs to that group. On Raspbian this is done with adduser.
Note that if you're not running the raspbian distribution, the names of the relevant commands might be different.
With this set-up every user that is part of the gpio group can access the GPIO devices.
Could you help me read some strings from MySQL and show them in my web browser.
This is just for my localhost.
I receive the following:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
As for my file hello.py, it is situated here:
root#ubuntu:/var/www/test1.my/www/cgi-bin# ls -l
total 8
It's properties are as follows:
-rwxr-xr-x 1 root root 465 Nov 2 23:30 hello.py
-rwxr-xr-x 1 root root 186 Nov 2 15:59 hello.py~
Could you give me some hint what to fix?
These are my configuration files
From what I see, you need to output an empty line after writing your Content-Type-header in hello.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
print "Content-Type: text/html\n"
# newline after headers! ^^
print "<html><head><title>Книги</title></head>"
...
Also, you should check the apache error logs (usually at /var/log/apache/error.log) to get more information about the error.