这几天在玩zturn开发板,发现开发板中的例程非常好玩,我就研究了下Linux下应用程序子启动问题,发现网上说法不一,下边总结一下我自己在Linux开发板下自动开机时启动应用程序的过程,与大家分享一下。
我们可以上网查阅Linux系统启动顺序,发现内核启动完毕,会执行init程序,启动新进程和退出时重新启动其它进程。init工作是在/etc/init.d/rcS文件里。
(关于rcS文件相关详细解说可以参考我博客里的文章http://blog.csdn.net/wendellluntan/article/details/72954379,这里不再赘述)
一、下面先看一下开发板里的关于rcS文件的配置都有些什么?
给开发板zturn上电,进入到linux启动界面,然后输入:cat /etc/init.d/rcS
#!/bin/sh
echo "Starting rcS..."
echo "++ Mounting filesystem"
mkdir -p /dev/pts
mkdir -p /dev/i2c
mount -a
echo "++ Setting up mdev"
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
# the device driver is not creating this node, so for now
# create the devcfg device node manually
if [ ! -e /dev/xdevcfg ]
then
mknod /dev/xdevcfg c 259 0
fi
echo "++ Starting telnet daemon"
telnetd -l /bin/sh
echo "++ Starting http daemon"
httpd -h /var/www
echo "++ Starting ftp daemon"
tcpsvd 0:21 ftpd ftpd -w /&
echo "++ Starting ssh daemon"
chmod 600 /etc/ssh_host_*
/usr/sbin/sshd
if [ -f /mnt/init.sh ]
then
echo "++ Running user script init.sh from SD Card"
source /mnt/init.sh
fi
echo "++ Try to start led-play daemon"
if [ -x /usr/bin/led-play ];
then
/usr/bin/led-play&
sleep 0.1
fi
echo "rcS Complete"
可以看出,此开发板预留了从sd卡启动用户脚本的选择语句。
if [ -f /mnt/init.sh ]
then
echo "++ Running user script init.sh from SD Card"
source /mnt/init.sh
fi
二、编写shell脚本程序:
借助启动脚本可以设置各种程序开机后自动运行,也可以设置其他系统设置,这有点类似于Windows 系统中的Autobat 自动批处理文件,启动脚本的位于板子的/etc/init.d/rcS,用户应用程序位于/mnt目录下。
此时,我们在/mnt目录下创建init.sh脚本,输入如下内容,(其中,脚本中的例程都是来自于开发板上的测试程序,经过交叉编译之后得到的):
#!/bin/sh
echo "++ Try to start led demon"
if [ -x /demoes/led-test ];
then
/demoes/led-test
fi
echo "++ Try to start btn demon"
if [ -x /demoes/btn-test ];
then
/demoes/btn-test
fi
echo "++ Try to start g-sensor demon"
if [ -x /demoes/gsensor-test ];
then
/demoes/gsensor-test
fi
echo "++ Try to start T-sensor demon"
if [ -x /demoes/tsensor-test ];
then
/demoes/tsensor-test
fi
保存之后,重新烧写SD卡image文件,然后重新上电,就可以看到:
Starting rcS...
++ Mounting filesystem
++ Setting up mdev
FAT-fs (mmcblk0p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
++ Starting telnet daemon
++ Starting http daemon
NET: Registered protocol family 10
++ Starting ftp daemon
++ Starting ssh daemon
random: sshd urandom read with 14 bits of entropy available
++ Running user script init.sh from SD Card
++ Try to start led demon
[usr_led1] Get trigger: 'none'
[usr_led1] Set trigger to 'none'
[usr_led2] Get trigger: 'none'
[usr_led2] Set trigger to 'none'
[ led_r] Get trigger: 'heartbeat'
[ led_r] Set trigger to 'none'
[ led_g] Get trigger: 'heartbeat'
[ led_g] Set trigger to 'none'
[ led_b] Get trigger: 'heartbeat'
[ led_b] Set trigger to 'none'
++ Try to start btn demon
++ Try to start g-sensor demon
++ Try to start T-sensor demon
++ Try to start led-play daemon
[2.596175] X: 0, Y: 0, Z: 228
[2.638643] X: 0, Y: 0, Z: 239
rcS Complete
Z-turn#
[2.681129] X: 0, Y: 0, Z: 238
[3.275842] X: 0,5 C Y: -3, Z: 238
[4.040538] X: 0,5 C Y: -3, Z: 237
[4.125436] X: 0, Y: -3, Z: 241
[4.507719] X: 0,5 C Y: -4, Z: 241
[4.805030] X: 0,5 C Y: -1, Z: 241
[4.974974] X: 0,5 C Y: -1, Z: 238
[5.357239] X: 0,5 C Y: -1, Z: 241
Temperature: 41.5 C
Temperature: 41.1 C
Temperature: 41.0 C
由此,我们可以看出,我们所希望的应用程序都已全部启动成功,并且都能够正常运行。