zoukankan      html  css  js  c++  java
  • linux c 查看其它程序是否启动。没有则启动他

    #include <sys/types.h>
    #include <dirent.h>
    #include<unistd.h>

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    #include "proc.h"

    /*
    * 读proc目录文件,查看所指定进程产生的文件是否存在
    */
    pid_t proc_find(const char* name) {
    DIR* dir;
    struct dirent* ent;
    char buf[512];

    long pid;
    char pname[100] = { 0, };
    char state;
    FILE *fp = NULL;

    if (!(dir = opendir("/proc"))) { //Open proc directory
    perror("can't open /proc");
    return -1;
    }

    while ((ent = readdir(dir)) != NULL) { //读proc目录文件
    long lpid = atol(ent->d_name);
    if (lpid < 0)
    continue;
    snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid);
    fp = fopen(buf, "r"); //打开指定目录文件

    if (fp) { // 如果能打开
    if ((fscanf(fp, "%ld (%[^)]) %c", &pid, pname, &state)) != 3) {
    printf("fscanf failed \n");
    fclose(fp);
    closedir(dir);
    return -1;
    }
    if (!strcmp(pname, name)) {
    fclose(fp);
    closedir(dir);
    return (pid_t) lpid;
    }
    fclose(fp);
    }
    }

    closedir(dir);
    return -1;
    }
    /*
    * proc 处理主函数, 做两件事
    * 1. 检查mysqld, vidireports 是否启动
    * 2.若没有启动,则启动他们
    */
    int proc(void) {

    /* if (argc == 1) {
    printf("usage: %s name1 name2 ...\n", argv[0]);
    return 1;
    }
    */
    int ret=0;
    #define apps_0 0x01
    #define apps_1 0x02

    #define MAX_CHECK_COUNTER 3

    char* apps[] = { "vidireports", "mysqld" };
    char* apps_start_cmds[] = { "bin/VidiReports/start.sh",
    "/etc/init.d/mysqd start" };
    int apps_has_started[] = { apps_0, apps_1 };

    int needCheck = apps_0 | apps_1;

    int n = sizeof(apps) / sizeof(apps[0]);
    int checkCounter = 0;
    while (needCheck && checkCounter++ < MAX_CHECK_COUNTER) { //进行核对,最多进行3次
    int i = 0;
    for (; i < n; i++) {
    if (!(needCheck & apps_has_started[i]))
    continue;
    pid_t pid = proc_find(apps[i]); //找出app[i]所指进程的ID
    if (pid == -1) { // 如果找不到所指定进程
    int status;
    printf("%s: not found\n", apps[i]);
    status = system(apps_start_cmds[i]);
    status = 0;
    printf("Example: %s: \n", apps_start_cmds[i]);
    if (-1 == status) {
    printf("system error!");
    ret = status;
    } else {
    printf("exit status value = [0x%x]\n", status);
    ret = status;
    if (WIFEXITED(status)) {
    if (0 == WEXITSTATUS(status)) {
    printf("run shell script successfully.\n");
    } else {
    printf(
    "run shell script fail, script exit code: %d\n",
    WEXITSTATUS(status));
    }
    } else {
    printf("exit status = [%d]\n", WEXITSTATUS(status));
    }
    }

    } else {
    needCheck ^= apps_has_started[i];
    printf("%s: %d\n", apps[i], pid);
    }
    } //for
    sleep(2);
    } //when need check
    return ret;
    }

  • 相关阅读:
    CGLib实现不同类中同名不同类型属性复制
    stream 伪复用实现
    年终盘点 | 2020年,国内私有云正式进入3.0时代
    高危端口135,137,138,139,445,1025,2475,3127,6129,3389,593
    在jsp引入js失败,提示404
    安全漏洞扫描
    Go 实现的文件行数统计工具
    .NET Core 获取域名 DNS 解析记录
    .NET Core 操作 Windows 注册表
    .NET MongoDb BsonDocument 序列化
  • 原文地址:https://www.cnblogs.com/bigben0123/p/2949419.html
Copyright © 2011-2022 走看看