/*********************************************************************************
|
* Copyright: (C) 2025 LingYun IoT System Studio
|
* All rights reserved.
|
*
|
* Filename: mydaemon.c
|
* Description: This file is daemon check example program
|
*
|
* Version: 1.0.0(11/07/2025)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "11/07/2025 10:38:39 AM"
|
*
|
********************************************************************************/
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <unistd.h>
|
#include <string.h>
|
#include <errno.h>
|
#include <signal.h>
|
#include <sys/types.h>
|
|
|
#define PID_FILE "/tmp/mydaemon.pid"
|
|
/* 检查某个 pid 是否存在 */
|
int is_process_running(pid_t pid)
|
{
|
if (pid <= 0)
|
return 0;
|
|
if (kill(pid, 0) == 0)
|
{
|
return 1; /* 进程存在且可访问 */
|
}
|
else
|
{
|
if (errno == ESRCH)
|
return 0; /* 进程不存在 */
|
else if (errno == EPERM)
|
return 1; /* 存在但无权限 */
|
else
|
return 0; /* 其他错误 */
|
}
|
}
|
|
/* 将当前进程 PID 写入文件 */
|
int write_pid_file(const char *path)
|
{
|
FILE *fp;
|
|
fp = fopen(path, "w");
|
if (fp == NULL)
|
{
|
perror("fopen");
|
return -1;
|
}
|
|
pid_t pid = getpid();
|
fprintf(fp, "%d\n", pid);
|
fclose(fp);
|
return 0;
|
}
|
|
/* 从文件中读取 PID */
|
pid_t read_pid_file(const char *path)
|
{
|
FILE *fp;
|
pid_t pid = 0;
|
|
fp = fopen(path, "r");
|
if (fp == NULL)
|
return 0;
|
|
fscanf(fp, "%d", &pid);
|
fclose(fp);
|
return pid;
|
}
|
|
int main(void)
|
{
|
pid_t old_pid;
|
|
old_pid = read_pid_file(PID_FILE);
|
if (is_process_running(old_pid))
|
{
|
printf("Program is already running with PID %d.\n", old_pid);
|
return 0;
|
}
|
|
/* 调用 daemon() 放入后台 */
|
/*
|
* daemon(int nochdir, int noclose)
|
* nochdir = 0 表示切换到根目录 /
|
* noclose = 0 表示关闭 stdin/stdout/stderr
|
*/
|
if (daemon(0, 0) < 0)
|
{
|
perror("daemon");
|
exit(1);
|
}
|
|
/* 写入新的 PID 文件 */
|
if (write_pid_file(PID_FILE) < 0)
|
{
|
fprintf(stderr, "Failed to write PID file.\n");
|
exit(1);
|
}
|
|
/* 模拟后台工作 */
|
while (1)
|
{
|
sleep(5); /* 这里可以换成自己的业务逻辑 */
|
}
|
|
return 0;
|
}
|