2

これが私のコードです

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <iostream>

int main(int argc, char *argv[]) {
    pid_t pid, sid;
    int sec = 10;

    pid = fork();
    if (pid < 0) {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    if (pid > 0) {
        std::cout << "Running with PID: " << pid << std::endl;      
        exit(EXIT_SUCCESS);
    }
    umask(0);        

    sid = setsid();
    if (sid < 0)
        exit(EXIT_FAILURE);

    if ((chdir("/")) < 0)
        exit(EXIT_FAILURE);    /* Log the failure */

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    while (1) {
        execl("/bin/notify-send", "notify-send", "-t", "3000", "Title", "body", NULL);
        sleep(sec); 
    }
    exit(EXIT_SUCCESS);
}

10秒ごとに通知したい。デーモンは正​​常に実行されていますが、通知はありません。

4

1 に答える 1

1

execl戻りません - 実行中のプログラムを新しいものに置き換えます。したがって、ループで実行してsleepも意味がありません。一度だけ実行されます。

system代わりに使うべきだと思います。コマンドを実行してリターンします。

別の方法はfork、ループのたびにexecl、親がループを続行している間に子に実行させることです。

于 2012-05-14T08:47:57.963 に答える