在Raspberry Pi上启动时难以启动python脚本

时间:2019-05-02 10:00:00

标签: python raspberry-pi boot

希望有人可以提供帮助。我是python的新手,正在尝试在raspbian上启动时启动脚本。我尝试的一切似乎都没有效果,只是看到我所缺少的东西。一个非常基本的脚本,用于在收到UDP命令时播放音频文件。

到目前为止,我已经尝试过-从rc.local启动,在.bashrc中启动(当我通过ssh启动新终端时,这项工作是从init.d开始,下面是init.d脚本,.py标准脚本是相同的减去Init信息。...

 <!DOCTYPE html>

<html>
<body>
<h3>Welcome to my page</h3>
</body>
</html>

I want to get a result like

Hi Guest
Welcome to my Page

如上所述,脚本起作用了,我只是无法让它自动启动并在启动时在后台运行?

提前谢谢...

尝试了以下一项cron作业:

#! /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*")

并添加了

sudo crontab -e 

也已将其设置为服务,如果我手动启动该服务,则它可以正常运行,但再次从引导启动则不能。

1 个答案:

答案 0 :(得分:1)

尝试创建服务。

打开shell并输入命令:sudo vi /etc/rc.local 这将打开一个包含以下详细信息的文件。

#!/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

给出脚本的路径,并将my_script.py替换为脚本名称。保存并退出文件。

它还将scipt的日志保存在桌面上的log.txt文件中。

如果这不起作用,请按如下所示修改脚本。

#! /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*")
相关问题