1

我在 Debian 10 Buster 的 Raspberry Pi 上运行了一个 python3 脚本,我可以在终端窗口中运行,但不能在启动时运行?

我在 /etc/profile 的文件末尾使用以下代码行

nohup python3 statlogger.py -db=stat_logger_db -sn=test

虽然代码行在我手动运行时在终端窗口中工作,但它在启动 pi 时不起作用

我也试过

nohup python3 statlogger.py & -db=stat_logger_db -sn=test

python代码需要在启动时运行,然后在pi开机时连续运行,任何想法

4

2 回答 2

2

我花了大约 1 天的时间测试了 4 个解决 Linux/raspberry 启动运行脚本问题的解决方案。那么这里是解决方案(我选择crontab)

  1. 打开 crontab (使用 linux@crontab -e -> 不使用 sudo crontab -e ...这可能是将来某些访问脚本等的错误)

pi@crontab -e

然后选择 nano 并像这样编辑......这很棘手,我在测试后发现可能超过 10 倍:

@reboot cd /home/pi/beetool && /usr/bin/python3 run.py &

我的代码在 /home/pi/beetool 下等着我。如果不改变目录就会发生错误。

而且我的代码使用无限循环,所以我必须用“&”结束,如上所示。

所以我希望这对某人也有帮助......

我还分享了:在 Raspberry Startup 上运行终端命令

于 2020-10-06T08:17:54.407 回答
0

我发现在启动时在 Raspberry Pi 上运行程序的最佳方法是使用 systemd 文件。

  1. 创建单元文件

使用以下命令打开一个新的示例单元文件:

sudo nano /lib/systemd/system/sample.service
  1. 将以下文本添加到新文件中。(请记住将 python 文件路径更改为您的文件):
[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

使用“CTRL+E”保存并退出,然后当要求保存“Y”和“Enter”时

  1. 将文件的权限设置为 644 :
sudo chmod 644 /lib/systemd/system/sample.service
  1. 使用以下命令告诉 systemd 在引导过程中启动它:
sudo systemctl daemon-reload
sudo systemctl enable sample.service
  1. 使用重启 Pi
sudo reboot now

来源: https ://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/#systemd

于 2020-10-06T10:40:20.160 回答