Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1038194
  • 博文数量: 836
  • 博客积分: 43880
  • 博客等级: 大将
  • 技术积分: 5485
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-20 17:36
文章分类

全部博文(836)

文章存档

2011年(1)

2008年(835)

我的朋友

分类: LINUX

2008-08-20 18:19:38


首先编写了一个用作服务的程序,功能很简单,每隔1秒钟把当前时间写入一个文件中:
voidrecordTime()
{
constcharpa[256]="//home//projects//testService//recordTime";
ofstreamfout;
fout.open(pa,ios::app);
time_tcurrTime;
structtm*tp;
charbuf[256];
while(1)
{
currTime=time(NULL);
tp=localtime(&currTime);
strftime(buf,256,"%B%e,%Y,%H:%M:%S",tp);
fout<<"currenttimeis"<
然后编译成可执行文件,我把它命名为:testService。 再在/etc/init.d下放一个脚本文件,这个文件里面包含了服务启动、关闭、重启等的函数实现:
start()
{
echo"starttestService"
/home/projects/testService/testService&
exit0;
}
stop()
{

echo-n"stoptestService"
ifpkilltestService
then

echo"[ok]"

else

echo"[failed]"

fi

}

case"$1"in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo"usage:$0start|stop|restart"
exit0;
esac
所有函数一目了然,调用start()函数在后台启动testService程序,stop()用来停止程序,restart()更是简单的执行了stop()、start()函数。当需要启动或停止服务的时候只需给程序一个start/stop的参数就行了。 这样,这个简单的服务就完成了。 (责任编辑:云子)


下载本文示例代码
阅读(177) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~