免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1088 | 回复: 0

一道很好的Shell编程题 [复制链接]

论坛徽章:
0
发表于 2009-05-09 12:19 |显示全部楼层
    最近在网上浏览到一个对于Shell编程题的讨论,因为感觉这个题目很好,所以整理如下:
请编写一个标准Shell脚本testd,实现如下功能:
A、在Linux操作系统启动的时候,自动加载/mnt/test/test程序。
B、当test退出之后,自动重新启动。
C、当test程序重启次数超过100次,自动复位操作系统。
假设你所拥有的资源:
A、目标机器是一台具有标准shell的嵌入式计算机,CPU为ARM7 56MB,内存16MB,软件环境基于Linux2.6.11和BusyBox1.2构建。
B、当前已有11个用户进程在运行,占用了大部分的CPU时间和内存,你可使用的内存只有2MB左右,CPU时间由系统分派。
(本题满分20分,程序15分,注释5分。程序不能运行的0分,能够运行但有BUG的10分,能够正确无误的运行的15分。清楚编写注释的5分。)
参考答案:
########################################
#testd is a daemon script to start and watch the program test
########################################
#!/bin/sh
#load *.so that may need
if [ -r /sbin/ldconfig ]; then
ldconfig
fi
#add the libs PATH that may need
export LD_LIBRARY_PATH="/lib"
#count is a counter of test running times
count=0
#main loop
while [ 1 ] ;do
#add execute attribute to /mnt/test/test
chmod +x /mnt/test/test
#launch test
/mnt/test/test
#the running times counter
let count=count+1
echo "test running times is $count"
#Is test runs too many times?
if [ "$count" -gt 100 ]; then
echo "Will reboot when test runs too many times"
reboot
fi
#waiting for test stop...
sleep 3
done
#########################################
最后,在init里加载这个脚本,实现系统启动时自动加载的功能。
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/77292/showart_1922964.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP