Reputation: 2277
I want to create a deamon in Linux that needs to kill off another process. I've created the deamon and it works fine, but my second process is created but it does not run as expected.
Is there something that I'm missing in starting the new process?
This is my code
void StartWSDevice()
{
pid_t pid;
int status;
fflush(NULL);
pid = fork();
switch (pid) {
case -1:
perror("fork");
break;
case 0: {
syslog(LOG_NOTICE, "WSDevice started.");
int res = execl("home/pi/projects/WSDevice/bin/ARM/Debug",
"WSDevice.out", (char *)0);
syslog(LOG_NOTICE, "WSDevice return %d.", res);
break;
}
default:
fflush(NULL);
if (waitpid(pid, &status, 0) != -1) {
syslog(LOG_NOTICE, "Child exited with status %i\n", status);
} else {
perror("waitpid");
}
break;
}
}
int main(void) {
deamonize();
syslog(LOG_NOTICE, "WSDeviceService started.");
while (!stopService) {
// Check if my child process is running
int pid_file = open("/var/run/WSDevice.pid",
O_CREAT | O_RDWR, 0666);
int rc = flock(pid_file, LOCK_EX | LOCK_NB);
if (rc) {
if (EWOULDBLOCK == errno) {
}
} else {
StartWSDevice(); // Its not running, start it
}
sleep(30); /* wait 30 seconds */
}
syslog(LOG_NOTICE, "WSDeviceService terminated.");
exit(EXIT_SUCCESS);
}
Upvotes: 0
Views: 53
Reputation: 992747
You're using execl
incorrectly. The first argument to execl()
is the process to run. The remaining arguments are the contents of the argv
array that is passed to the process. The key here is that argv[0]
should be the name of the process being run. So:
int res = execl("/home/pi/projects/WSDevice/bin/ARM/Debug/WSDevice.out",
"/home/pi/projects/WSDevice/bin/ARM/Debug/WSDevice.out",
NULL);
Note that I've also inserted /
in front of home
. This may be important for you. I've also used NULL
instead of (char *)0
, which is more of a style thing and doesn't change the functionality.
Upvotes: 1