[英]How to let linux(raspberry) run python script when plugin power?
I have a python script( namely /home/pi/x.py
). 我有一个python脚本(
namely /home/pi/x.py
)。 And I have tried a lot of ways to let the program, some thing such as: 我尝试了很多方法让程序运行,例如:
#!/bin/bash
# /etc/init.d/mystart
### BEGIN INIT INFO
# Provides: Python
(I am thinking am I right here(Provides: Python)?)
(我在想我在这里(提供:Python)吗?)
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: my python initscript
# Description: no description
### END INIT INFO case "$1" in
start)
echo "Starting x.py "
/home/pi/x.py &
;;
stop)
echo "Stopping x.py"
#killall x.py
kill $(ps aux | grep -m 1 'python /home/pi/x.py' | awk '{ print $2 }')
;;
*)
echo "Usage: service x.py start|stop"
exit 1
;; esac exit 0
I have modified this bash from its original form, and put it in 我已经从原始形式修改了此bash,并将其放入
/etc/inti.d/mystart
sudo chmod +x /etc/init.d/mystart
sudo update-rc.d mystart defaults
However, when I try to: sudo service mystart start
但是,当我尝试执行以下操作时:
sudo service mystart start
Some error comes out! 出现一些错误!
[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
所以我在这里被封锁了,我不知道如何在通电的情况下让x.py运行
Open /etc/profile 打开/ etc / profile
sudo nano /etc/profile
Scroll to the bottom and add the following line : 滚动到底部并添加以下行:
sudo python /home/pi/x.py
where “/home/pi/x.py” is the absolute path to your script. 其中“ /home/pi/x.py”是脚本的绝对路径。 Type “Ctrl+X” to exit, then “Y” to save followed by “Enter” twice.
键入“ Ctrl + X”退出,然后键入“ Y”保存,然后按两次“ Enter”。 Now reboot and test.
现在重新启动并测试。 python script should now run on startup
python脚本现在应该在启动时运行
Auto Login Setup(to execute script without any intervention from user) 自动登录设置(无需用户干预即可执行脚本)
Open /etc/inittab 打开/ etc / inittab
sudo nano /etc/inittab 须藤nano / etc / inittab
Find this line 找到这条线
1:2345:respawn:/sbin/getty 115200 tty1 1:2345:重生:/ sbin / getty 115200 tty1
Add a # character to the beginning of the line to disable it so it looks like 在行首添加#字符以禁用它,因此它看起来像
#1:2345:respawn:/sbin/getty 115200 tty1 #1:2345:respawn:/ sbin / getty 115200 tty1
Under that line, add the following: 在该行下,添加以下内容:
1:2345:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1 1:2345:respawn:/ bin / login -f pi tty1 / dev / tty1 2>&1
where “pi” is the username. 其中“ pi”是用户名。 Type “Ctrl+X” to exit, then “Y” to save followed by “Enter” twice.
键入“ Ctrl + X”退出,然后键入“ Y”保存,然后按两次“ Enter”。 Now on startup, raspberrypi will autologin with
pi
user and execute your script 现在启动时,raspberrypi将使用
pi
用户自动登录并执行您的脚本
What if you don't have /etc/inittab? 如果没有/ etc / inittab怎么办?
I assume you're using the latest Raspian-Image (jessie). 我假设您使用的是最新的Raspian图片(jessie)。 This one is based on Debian 8 (jessie) where the init-system changed.
这是基于Debian 8(jessie)更改了init系统的版本。 Autologin solution is already mentioned here
自动登录解决方案已经在这里提到
Source: http://www.raspberrypi-spy.co.uk/2015/02/how-to-autorun-a-python-script-on-raspberry-pi-boot/ 来源: http : //www.raspberrypi-spy.co.uk/2015/02/how-to-autorun-a-python-script-on-raspberry-pi-boot/
I used crontab
and it works well 我使用了
crontab
并且效果很好
Step 1: 第1步:
sudo crontab -e
Step 2: fill in it 步骤2:填写
@reboot python /home/pi/x.py &
Step 3: Save it and reboot 步骤3:保存并重新启动
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.