-
Notifications
You must be signed in to change notification settings - Fork 612
/
Linux_OS_Daemon.cpp
55 lines (49 loc) · 1.23 KB
/
Linux_OS_Daemon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <sys/stat.h>
#include <fcntl.h>
// 守护进程初始化函数
void init_daemon()
{
pid_t pid;
int i = 0;
if ((pid = fork()) == -1) {
printf("Fork error !\n");
exit(1);
}
if (pid != 0) {
exit(0); // 父进程退出
}
setsid(); // 子进程开启新会话,并成为会话首进程和组长进程
chdir("/tmp"); // 改变工作目录
umask(0); // 重设文件掩码
for (; i < getdtablesize(); ++i) {
close(i); // 关闭打开的文件描述符
}
return;
}
int main(int argc, char *argv[])
{
int fp;
time_t t;
char buf[] = {"This is a daemon: "};
char *datetime;
int len = 0;
// 初始化 Daemon 进程
init_daemon();
// 每隔一分钟记录运行状态
while (1) {
if (-1 == (fp = open("/tmp/daemon.log", O_CREAT|O_WRONLY|O_APPEND, 0600))) {
printf("Open file error !\n");
exit(1);
}
len = strlen(buf);
write(fp, buf, len);
t = time(0);
datetime = asctime(localtime(&t));
len = strlen(datetime);
write(fp, datetime, len);
close(fp);
sleep(60);
}
return 0;
}