<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]温控制系统之让树莓派 Raspberry Pi开机启动一个脚本/执行一个命令怎么办？用mplayer播放声音。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[系统架构与硬件]]></category>
<pubDate>Sun, 28 Jun 2015 07:25:02 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：如果你想在树莓派开机时做一些外围设备的自检，如串口是否连接上，如手机是否连接上adb命令，这样就需要开机后启动一个脚本来做这个事情，于是有这篇文章。<br/>实践如下：<br/>vi /etc/rc.local<br/>/usr/bin/php /var/www/initOuterInterfaceCheck.php<br/>initOuterInterfaceCheck.php<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php
$voicePath=&quot;/var/www/voice/&quot;;//播放声音目录
$command = &quot;/sbin/adb devices&quot;; //电话号码可修改,防止手机丢了。
$handle = popen($command, &quot;r&quot;);
$excuteResultAll = &quot;&quot;;
do &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$excuteResult = fread($handle, 1024);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (strlen($excuteResult) == 0) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pclose($handle); //关闭pipe
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$excuteResultAll .= $excuteResult;
&#125; while (true);
$adbDevicesArrInfo = explode(&quot;&#92;n&quot;,$excuteResultAll);
if(empty($adbDevicesArrInfo[1]))&#123;//如果没有设备，就提示用户插上手机即可。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$command = &quot;mplayer &quot;.$voicePath.&quot;initMobileBad.m4a &quot;; //电话号码可修改,防止手机丢了。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$handle = popen($command, &quot;r&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$excuteResultAll = &quot;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$excuteResult = fread($handle, 1024);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (strlen($excuteResult) == 0) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pclose($handle); //关闭pipe
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$excuteResultAll .= $excuteResult;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; while (true);


&#125;else&#123;//识别到手机设备
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$command = &quot;mplayer &quot;.$voicePath.&quot;initMobileOk.m4a &quot;; //电话号码可修改,防止手机丢了。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$handle = popen($command, &quot;r&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$excuteResultAll = &quot;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$excuteResult = fread($handle, 1024);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (strlen($excuteResult) == 0) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pclose($handle); //关闭pipe
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$excuteResultAll .= $excuteResult;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; while (true);


&#125;
?&gt;
</textarea><br/>发现是代码有问题，开机后，的确是执行了，但是发现手机没插入，但报检测到手机了~<br/>要让Pi开机启动一个脚本/执行一个命令怎么办？<br/>我们知道，RedHat有 /etc/rc.local 文件，在里面写上要执行的命令就可以开机执行了，这是最简单的办法，而Arch Linux ARM没有这个东西，它是按下面的方法设置的：<br/>假设我要开机执行一句shell命令，把它写在文件 /etc/rc.local 中（在Arch Linux中，此文件一开始是不存在的）：<br/>How to execute a shell script/a command on Pi startup?<br/>For RedHat it&#039;s very easy, it has a /etc/rc.local file, we just add the commands to the file, while Arch Linux ARM doesn&#039;t have such thing, and I&#039;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 &amp; write the shell command to it:<br/><br/>#!/bin/bash<br/># this file defines the commands that will be executed at system startup<br/> <br/>echo &quot;abc&quot; &gt; /root/test.txt<br/>为此文件赋予可执行权限：<br/>Give the file the executable permission:<br/><br/>1<br/>chmod +x /etc/rc.local<br/>然后创建一个文件 /usr/lib/systemd/system/rc-local.service ，内容为：<br/>Then create a file /usr/lib/systemd/system/rc-local.service , with the content of:<br/><br/>[Unit]<br/>Description=/etc/rc.local Compatibility<br/>ConditionPathExists=/etc/rc.local<br/>&nbsp;&nbsp;<br/>[Service]<br/>Type=forking<br/>ExecStart=/etc/rc.local start<br/>TimeoutSec=0<br/>StandardOutput=tty<br/>RemainAfterExit=yes<br/>SysVStartPriority=99<br/> <br/>[Install]<br/>WantedBy=multi-user.target<br/>文章来源：http://www.codelast.com/<br/>创建一个软链接：<br/>Create a symbol link:<br/><br/>cd /etc/systemd/system/multi-user.target.wants<br/>ln -s /usr/lib/systemd/system/rc-local.service rc-local.service<br/>启用服务：<br/>Enable the service:<br/><br/>systemctl enable rc-local.service<br/>重启系统，然后就可以开机执行你的shell命令了。如果要立即启用，则执行以下命令：<br/>Restart Pi, then you&#039;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:<br/><br/>systemctl start rc-local.service<br/>这是使用systemd的启动方式，非常麻烦。<br/>如果你只是简单地要设置一个命令的别名，可以把命令写在 /etc/profile 中，ssh登录Pi的时候就自动会执行。<br/><br/>摘自：http://www.codelast.com/?p=4945
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]温控制系统之让树莓派 Raspberry Pi开机启动一个脚本/执行一个命令怎么办？用mplayer播放声音。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>