[实践OK]温控制系统之让树莓派 Raspberry Pi开机启动一个脚本/执行一个命令怎么办?用mplayer播放声音。

jackxiang 2015-6-28 15:25 | |
背景:如果你想在树莓派开机时做一些外围设备的自检,如串口是否连接上,如手机是否连接上adb命令,这样就需要开机后启动一个脚本来做这个事情,于是有这篇文章。
实践如下:
vi /etc/rc.local
/usr/bin/php /var/www/initOuterInterfaceCheck.php
initOuterInterfaceCheck.php

发现是代码有问题,开机后,的确是执行了,但是发现手机没插入,但报检测到手机了~
要让Pi开机启动一个脚本/执行一个命令怎么办?
我们知道,RedHat有 /etc/rc.local 文件,在里面写上要执行的命令就可以开机执行了,这是最简单的办法,而Arch Linux ARM没有这个东西,它是按下面的方法设置的:
假设我要开机执行一句shell命令,把它写在文件 /etc/rc.local 中(在Arch Linux中,此文件一开始是不存在的):
How to execute a shell script/a command on Pi startup?
For RedHat it's very easy, it has a /etc/rc.local file, we just add the commands to the file, while Arch Linux ARM doesn't have such thing, and I'm a beginner of Arch, so after asking Google for many times, I find a way to do that - suppose I need to execute a shell command, so I create the /etc/rc.local file & write the shell command to it:

#!/bin/bash
# this file defines the commands that will be executed at system startup

echo "abc" > /root/test.txt
为此文件赋予可执行权限:
Give the file the executable permission:

1
chmod +x /etc/rc.local
然后创建一个文件 /usr/lib/systemd/system/rc-local.service ,内容为:
Then create a file /usr/lib/systemd/system/rc-local.service , with the content of:

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
  
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
文章来源:http://www.codelast.com/
创建一个软链接:
Create a symbol link:

cd /etc/systemd/system/multi-user.target.wants
ln -s /usr/lib/systemd/system/rc-local.service rc-local.service
启用服务:
Enable the service:

systemctl enable rc-local.service
重启系统,然后就可以开机执行你的shell命令了。如果要立即启用,则执行以下命令:
Restart Pi, then you'll be able to execute the shell command on OS startup. If you want to execute it without restarting Pi, just execute the following command:

systemctl start rc-local.service
这是使用systemd的启动方式,非常麻烦。
如果你只是简单地要设置一个命令的别名,可以把命令写在 /etc/profile 中,ssh登录Pi的时候就自动会执行。

摘自:http://www.codelast.com/?p=4945

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/8146/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2015-8-17 21:36
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]