Here is my code
#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);
}
I want it to give a notification in every 10 sec. Daemon is running OK, but not giving any notification.
execldoesn’t return – it replaces the running program with a new one. So running it in a loop withsleepis meaningless – it will run only once.I think you should use
systeminstead. It executes a command and returns.The alternative is to
forkeach time in the loop, and have the child doexeclwhile the parent would continue the loop.