配置并运行蓝牙后运行脚本 - Rasperry Pi 3

K. *_*ven 2 bluetooth raspberry-pi raspbian raspberry-pi3

我正在尝试在启动时使用蓝牙自动调用程序.但是,在配置和运行蓝牙之前,将调用该程序.

我试过用两种方式调用程序:

  1. 使用init.d中的脚本并使用update-rc.d在init中使用此行注册: # Required-Start: $all
  2. 从/etc/rc.local调用它

这些都不能按预期工作.它们都启动程序,但在配置并运行蓝牙之前.

在蓝牙之后强制脚本或程序运行的最佳方法是什么?

下面是引导顺序中的一些选择行,因此您可以看到我遇到的问题:

[ OK ] Started Login Service.
[ OK ] Started Getty on tty1.
**Where my program is currently executing**
[ OK ] Started Configure Bluetooth Modems connected by UART.
[ OK ] Reached Target Bluetooth
**Where I want my program to be executing**

Raspbian GNU/Linux 8 tty1
login:
Run Code Online (Sandbox Code Playgroud)

and*_*pei 6

Debian 8"Jessie"的新init系统是systemd.在Debian 7"Wheezy"中的旧方式是具有运行级别的Sysv /etc/inittab.使用crontab运行程序的一个缺点是,如果脚本执行崩溃,它将永远消亡.如果脚本结束,则自动重新启动脚本称为"respawn".

如您所见,蓝牙服务正在运行并打印到达"目标".要创建自己的服务,在蓝牙启动后运行,并使用systemd重新生成,只需在/etc/systemd/system/ie中创建一个文件my_program.service

[Unit]
Desription=my_program with systemd, respawn, after bluetooth
After=bluetooth.target

[Service]
ExecStart=node /home/pi/workspace/my_program
Restart=always

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

并激活它

systemctl enable my_program.service
Run Code Online (Sandbox Code Playgroud)

重启或手动启动它

systemctl daemon-reload
systemctl start my_program.service
Run Code Online (Sandbox Code Playgroud)

如果一个人终止进程或重新启动,my_program将在几秒钟后自动重启.