linux写系统服务的方法
2.1 首先编写demo程序:hello.c
#include
# chkconfig: 2345 10 90
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
2.7重启服务器
shutdown -r now
原文:https://www.cnblogs.com/newmiracle/p/11856339.html