无法让我的 python 脚本在启动时/从树莓派上的快捷方式运行 [英] Cannot get my python script to run on startup/from a shortcut on raspberry pi

查看:87
本文介绍了无法让我的 python 脚本在启动时/从树莓派上的快捷方式运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 python 脚本,我想用它至少做这两件事之一:

So I have a python script with which I would like to do at least one of these two things:

  1. 让它在启动/登录时自动运行
  2. 创建一个运行脚本的快捷方式

我已经尝试了我能找到的每一种方法来完成这些,但似乎每次问题都或多或少相同.最初看起来好像程序正在运行,由处理器活动的大幅增加表明,但这只会持续几秒钟,然后 pi 返回到什么都不做.如果终端打开,它不会显示任何消息.该脚本会打开一个 pygame 窗口,有时(取决于所使用的方法)该窗口会在关闭之前显示几秒钟.

I have tried every method I could find to do each of these, but it seems the problem is more or less the same each time. Initially it looks as if the program is running, indicated by a big surge in processor activity but this only lasts for a few seconds before the pi goes back to doing nothing. If a terminal opens it shows no message whatsoever. The script opens a pygame window and sometimes (depending on the method used) this window will show up for a few seconds before closing itself.

该脚本相当复杂,因为它使用了其目录中的图像和模块以及 GPIO 引脚.

The script is fairly compicated in that it makes use of images and modules from its directory as well as the GPIO pins.

如果我尝试从终端窗口运行脚本,它只有在我 cd 进入其目录时才能正常工作,否则它只是说它无法导入图像并挂起.(sudo 似乎对它是否有效没有任何影响)

If I try to run the script from the terminal window it only works properly if I cd into its directory, otherwise it simply says it cannot import an image and hangs. (sudo doesn't seem to make any difference as to whether or not it works)

我假设这意味着如果我在运行之前设法让 rpi 移动到脚本的目录中它会起作用,但是我不确定这是否可以通过快捷方式或任何自动启动"方法实现

I'm assuming this means it would work if I managed to get the rpi to move into the script's directory before running it, however I'm not sure if this is possible through shortcuts or any "autostart" method

如果不让我知道,希望这一切都清楚

Hopefully this is all clear if not let me know

推荐答案

您必须编写简单的脚本来运行您的代码,如下所示:

You must write easy script to run your code like this one :

例如在/opt 中创建目录:

For example create directory in /opt :

cd /opt
mkdir my_bootup_script
cd my_bootup_script
nano my_python_script.py

然后写python代码.对于这个例子,我试过这个:

Then write python code. For this example i tried this :

#!/usr/bin/python
import sys
import Adafruit_DHT
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
if humidity is not None and temperature is not None:
    print 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity)
else:
    print 'Failed to get reading. Try again!'

然后在同一文件夹中制作 bash 脚本,例如:

Then make in same folder bash script for example :

touch my_script_runner.sh
chmod a+x my_script_runner.sh
nano my_script_runner.sh

然后编写bash脚本.对于这个例子,我试过这个:

Then write bash script. For this example i tried this :

#!/bin/bash
cd /opt/my_bootup_script
sudo /usr/bin/python ./my_python_script.py
exit

试试看是否运行良好:./my_script_runner.sh

Try if it is working well : ./my_script_runner.sh

如果是,将此脚本添加为启动脚本:

If yes add this script as boot script :

sudo nano /etc/rc.local
# Add Line :
nohup sudo /opt/my_bootup_script/my_script_runner.sh >> /opt/my_bootup_script/my_script_run.log
# Save with ctrl + x and y

重启 R-Pi:sudo reboot 并查看文件夹/opt/my_bootup_script/以获取启动脚本生成的 my_script_run.log 以获取信息.这个例子在我的 R-Pi 上运行良好.Python 脚本在第 4 个 GPIO 引脚上使用带有温度和湿度传感器 DHT11 的 Adafruit 模块.

Restart R-Pi : sudo reboot and look into folder /opt/my_bootup_script/ for my_script_run.log generated by bootup script for information. This example worked well on my R-Pi. Python script use Adafruit module with Temperature And Humidity Sensor DHT11 on 4th GPIO Pin.

这篇关于无法让我的 python 脚本在启动时/从树莓派上的快捷方式运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆