赞
踩
添加到rc.local 里的py脚本文件将会在树莓派开机是自动运行,这种方式适合没有显示器,具体操作如下:
sudo nano /etc/rc.local
sudo reboot
该方法在登录时运行(启动并直接进入桌面时自动运行),以及每次打开一个新终端或建立一个新的SSH连接时也会运行。把你的命令放在’ /home/pi/.bashrc ‘的底部。程序可以在运行时用’ ctrl-c '终止!,具体步骤
sudo nano /home/pi/.bashrc
echo Running at boot
sudo python3 /home/pi/sample.py
这个目录包含在引导过程中启动的脚本(这里的所有程序都是在关闭或重新引导系统时执行的)。
sudo chmod 777 sample.py
cd /etc/init.d
sudo touch blinker
sudo nano blinker
#!/bin/bash # /etc/init.d/blinker ### 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: blinker initscript # Description: This service is used to manage a led ### END INIT INFO case "$1" in start) echo "Starting LED Blink" nohup python3 /home/pi/sample.py & ;; stop) echo "Stopping ledblink" #killall ledblink.py #kill $(ps aux | grep -m 1 'python /home/pi/sample.py' | awk '{ print $2 }') ;; *) echo "Usage: service ledblink start|stop" exit 1 ;; esac exit 0
sudo chmod 777 blinker
sudo update-rc.d blinker defaults
sudo service blinker start
sudo reboot
sudo service blinker stop
sudo update-rc.d blinker remove
systemd提供了一个标准进程来控制在Linux系统启动时运行的程序。注意,systemd只在Raspbian OS的Jessie版本中可用
sudo nano /lib/systemd/system/sample.service
[Unit]
Description=My Sample Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/sample.py
[Install]
WantedBy=multi-user.target
sudo chmod 644 /lib/systemd/system/sample.service
sudo systemctl daemon-reload
sudo systemctl enable sample.service
sudo reboot
sudo systemctl stop sample.service
sudo systemctl disable sample.service
sudo crontab -e
@reboot sudo python3 /home/pi/Desktop/sample.py
/home/pi/Desktop/log.txt
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。