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