0

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..

7
  • Can't you just make it a cron job that starts on boot? Commented May 2, 2019 at 10:03
  • can you check this and this Commented May 2, 2019 at 10:14
  • @Jeril Thanks that guide is actually the one i have followed on the 5 ways to boot, but nothing seems to run it unless i start a terminal. Commented May 2, 2019 at 10:18
  • @roganjosh I have made a cron job, but again it doesnt start n the background, maybe missing something in my script to make it a background task? Commented May 2, 2019 at 10:32
  • How did you make your cron job? Commented May 2, 2019 at 10:33

1 Answer 1

2

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*")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank-you this worked adding the sleep to the script. It does slowdown the other services booting and not sure how to combat that but i will have a play with it. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.