Linux 学习第一篇

来源:互联网 发布:金庸群侠传x源码 编辑:程序博客网 时间:2024/06/05 20:05
其实小一志在不是做Linux 管理员,学习的原因先不说了,以免笑话。今天第一篇,发个Linux守护进程的。实在惭愧,没有什么技术含量。乱贴代码和截图。旨在千里之行,始于足下!
自创的一个守护进程,朋友们自己家尖括号吧:

#include  stdio.h 
#include  unistd.h
#include  stdlib.h
#include  sys/types.h
#include  sys/stat.h
#include fcntl.h
#include string.h

int main(){
 int fd , i;
 char *content ="yiran\n";

 pid_t pid = fork(); //create process , exitparent

 if(pid<0) exit(1);
 else if(pid>0) exit(0);

 setsid();

 for(i=0;i<10076; i++) close(i); //close all fd 

 chdir("/"); // change dir

 umask(0); // initumask 

 while(1){   // dotask 
 
     fd =open("/tmp/mylog.log",O_CREAT|O_RDWR|O_APPEND,06666);
    if(fd>0){
  write(fd,content,strlen(content)); 
           close(fd); 
 }
    sleep(3);
  }

}


执行操作:
将该文件编译后放到home/yiran(可自定义) 下
找到etx/init.d 下的rc 文件 vi 打开 修改在Path下添加如下路径:/home/yiran/logd(logd为上述文件编译后的文件名)
保存退出 
reboot Linux

用 ps -ef | more  便可查到自己的写的守护进程。小一建议:
1:进程号靠前,多按几次空格。 
2:代码里的5行注释比较重要。  

0 0
原创粉丝点击