赞
踩
周六黑客马拉松做了个树莓派遥控船,发现简单的把python脚本加到rc.local好像无法自动启动,于是找到了下面的方法,做成一个服务。
文件保存在/home/pi/script/ledblink.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#!/usr/bin/env python
import
RPi.GPIO as GPIO
import
time
GPIO.setmode(GPIO.BCM)
GPIO.setup(
21
,GPIO.OUT)
while
True
:
try
:
GPIO.output(
21
,
True
)
time.sleep(
1
)
GPIO.output(
21
,
False
)
time.sleep(
1
)
except
(KeyboardInterrupt, SystemExit):
GPIO.close()
print
"exit"
|
保存脚本为/etc/init.d/ledblink文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#!/bin/bash
# /etc/init.d/ledblink
### BEGIN INIT INFO
# Provides: embbnux
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ledblink initscript
# Description: This service is used to manage a led
### END INIT INFO
case
"$1"
in
start)
echo
"Starting LED Blink"
/home/pi/script/ledblink
.py &
;;
stop)
echo
"Stopping ledblink"
#killall ledblink.py
kill
$(
ps
aux |
grep
-m 1
'python /home/pi/script/ledblink.py'
|
awk
'{ print $2 }'
)
;;
*)
echo
"Usage: service ledblink start|stop"
exit
1
;;
esac
exit
0
|
1
|
sudo
chmod
+x
/etc/init
.d
/ledblink
|
这样启动改脚本用service 命令就可以
1
2
|
sudo
service ledblink start
#启动
sudo
service ledblink stop
#停止
|
最后设置开机启动就好了
1
|
sudo
update-rc.d ledblink defaults
|
这样就完工了,重启树莓派就会发现led自己闪烁了,停止用sudo service ledblink stop就行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。