Reputation: 17
hoping someone can help. i'm new to python and trying to start a script on boot on raspbian. Nothing i try seems to work and just seeing what I am missing. A very basic script to play an audio file when it receives a UDP command.
I have so far tried - starting it from rc.local, starting it in .bashrc (this work when i start a new terminal via ssh, staring from init.d, below is the init.d script, the .py standard script is the same minus the Init info....
#! /usr/bin/python3
# /etc/init.d/UDP_Python_Omxplayer.py
### BEGIN INIT INFO
# Provides: UDP_Python_Omxplayer.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
import socket
import os
UDP_IP = "192.168.123.10"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print ("received message:", data)
command = str(data.decode('ASCII'))
if command == "Play":
os.system("omxplayer -o both --no-osd /home/pi/Doc*/Air*")
As above, the scripts work, i just cannot get this to autmate and run int he background on boot?
thanks in advance...
Have tried in a cron job with the following:
sudo crontab -e
and added
@reboot sudo python /home/pi/UDP_Python_Omxplayer.py
also have made this into a service, which if i start the service manually then it works fine, but again from boot does not..
Upvotes: 0
Views: 597
Reputation: 136
Try to create a service.
Open shell and type a command: sudo vi /etc/rc.local This will open a file including following detail.
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
cd /home/pi/XXXXX/XXXXX && python3 my_script.py > /home/pi/Desktop/log.txt 2>&1
exit 0
Give the path of your script and replace my_script.py with your script name. Save and exit the file.
It will also save the logs of scipt on desktop in log.txt file.
If this not works modify your script like below.
#! /usr/bin/python3
# /etc/init.d/UDP_Python_Omxplayer.py
### BEGIN INIT INFO
# Provides: UDP_Python_Omxplayer.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
from time import sleep
sleep(45)
import socket
import os
UDP_IP = "192.168.123.10"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print ("received message:", data)
command = str(data.decode('ASCII'))
if command == "Play":
os.system("omxplayer -o both --no-osd /home/pi/Doc*/Air*")
Upvotes: 2