如何让linux(raspberry)在插件供电时运行python脚本?

2024-10-05 10:53:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本(namely /home/pi/x.py)。我尝试了很多方法让程序,比如:

#!/bin/bash
# /etc/init.d/mystart  
### BEGIN INIT INFO
# Provides: Python 

(I am thinking am I right here(Provides: Python)?)

^{pr2}$

我已经修改了这个bash的原始形式,并将其放入

/etc/inti.d/mystart

sudo chmod +x /etc/init.d/mystart

sudo update-rc.d mystart defaults

但是,当我试图:sudo service mystart start

出了点差错! [Unit mystart.service failed to load: No such file or directory.]

So I'm blocked here, I dont know how to let x.py run while it the power is on


Tags: topy脚本bashhomehereinitservice
2条回答

我使用了crontab,它工作得很好

第一步:

sudo crontab -e

第2步:填写

^{pr2}$

第3步:保存并重新启动

打开/etc/profile

sudo nano /etc/profile

滚动到底部并添加以下行:

^{pr2}$

其中“/home/pi/x.py”是指向脚本的绝对路径。 键入“Ctrl+X”退出,然后键入“Y”保存,然后键入“Enter”两次。现在重新启动并测试。python脚本现在应该在启动时运行

自动登录设置(无需用户干预即可执行脚本)

  • 打开/etc/inittab

    sudo nano/etc/inittab

  • 找到这条线

    1:2345:重生:/sbin/getty 115200 tty1

  • 在行首添加一个#字符以禁用它,使其看起来像

    2月1日

  • 在该行下,添加以下内容:

    1:2345:重生:/bin/login-f pi tty1/dev/tty1 2>;1

其中“pi”是用户名。键入“Ctrl+X”退出,然后键入“Y”保存,然后键入“Enter”两次。现在启动时,raspberrypi将自动登录pi用户并执行脚本

如果没有/etc/inittab怎么办?

我想你用的是最新的Raspian图像(杰西)。这一个基于Debian8(jessie),其中init系统发生了变化。已提到自动登录解决方案here

来源:http://www.raspberrypi-spy.co.uk/2015/02/how-to-autorun-a-python-script-on-raspberry-pi-boot/

相关问题 更多 >

    热门问题