1、首先寫個簡單的python 腳本
腳本很簡單,就是樹莓派上一個燈閃爍程序,須要學gpio能夠看我以前的博客
文件保存在/home/pi/script/ledblink.pypython
- #!/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"
啓動腳本能夠是python,能夠是shell,也能夠是C語言C++編譯的可執行文件。
shell
二 、開機啓動腳本
保存腳本爲/etc/init.d/ledblink文件bash
- #!/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
複製代碼ide
3、設置python腳本開機啓動ui
- sudo chmod +x /etc/init.d/ledblink
複製代碼ip
這樣啓動改腳本用service 命令就能夠rem
- sudo service ledblink start#啓動
- sudo service ledblink stop#中止
複製代碼博客
最後設置開機啓動就行了it
- sudo update-rc.d ledblink defaults
複製代碼io
這樣就完工了,重啓樹莓派就會發現led本身閃爍了,中止用sudo service ledblink stop就行。