风之舞原创,转载请注明来源。
#include
#include
#include
#include
#include
#ifndef NOFILE
#define NOFILE 3
#endif
void init_daemon( void )
{
pid_t pid;
int i;
if ( (pid = fork()) == -1 )
exit(1);
if ( pid > 0 )
exit(0);
setsid();
if ( (pid = fork()) == -1 )
exit(1);
if ( pid > 0 )
exit(0);
for( i = 0 ; i< NOFILE; ++i )
close(i);
chdir("/tmp");
umask(0);
return;
}
int main ( void )
{
FILE *fp;
FILE *p;
char buf[256];
init_daemon();
while(1)
{
sleep(30);
if ( (fp = fopen("test.log","ab")) != 0 )
{
p = popen("ps -ef","r");
while(fgets(buf,sizeof(buf),p) != 0)
fprintf(fp,"%s\n",buf);
pclose(p);
}
fclose(fp);
}
exit(0);
}
阅读(1435) | 评论(0) | 转发(0) |