0
我想在Linux中創建一個需要關閉另一個進程的deamon。我已經創建了deamon並且工作正常,但我的第二個進程已創建,但未按預期運行。從deamon創建一個分叉進程
在啓動新過程中是否存在缺少的某些內容?
這是我的代碼
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);
}
通過它的名字,我也猜測'Debug'是一個目錄,而不是二進制文件,這將是execl的另一個錯誤用法。 – hvd
WSDevice.out是二進制文件,Debug是一個目錄是 – Jaques
@Jaques:我已經更新了我的答案,以說明您的新信息。 –