linux写系统服务的方法

linux写系统服务的方法
2.1 首先编写demo程序:hello.c
#include   
  
main()  
{  
    FILE *fp;  
    char a[] = "Hello world!";   
    fp=fopen("hhh.txt","a+");   
    fputs(a,fp);  
    return 0;  
}  
2.2 编译hello.c
gcc -g hello.c -o hello  
2.3 在/etc/init.d目录下添加脚本test
#!/bin/bash  
  
start(){  
    echo "------------------test----------------"  
    cd /home/xxx   //hello的所在文件夹的绝对路径  
    ./hello  
}  
  
case $1 in  
start):  
start  
;;  
stop):  
echo "-----------------stop------------------"  
;;  
esac  
  
exit 0 
2.4 设置权限
chmod 777 /etc/init.d/test  
2.5 利用service启动hello
service test start  
2.6 设置开机自动启动
chkconfig --add test   
chkconfig test on/off    //重启后永久生效  


查看原文:http://newmiracle.cn/?p=2373
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章