我在 init.d 中有一個守護進程,除了名稱和描述之外,其結構與標準 ubuntu 完全相同骨架檔案。當我嘗試使用運行所述守護程序時
sudo /etc/init.d/mydaemon start
我收到一條錯誤訊息,啟動守護程序失敗並顯示以下訊息
Control process exited, code=exited, status=1/FAILURE
這並不是很有幫助,因為據我所知,代碼 1 並沒有真正的意義。在偵錯這個過程時,我有一次決定將 /lib/init/vars.sh 中的詳細變數從 no 更改為 yes,只是為了引發一些輸出,並且在執行此操作後,守護程序將完美運行。然而,當我將 verbose 改回 no 時,我會遇到與以前相同的錯誤。你們有人遇到過類似的事情嗎?
另外,守護程式程式碼是用 C++ 編寫的,如下所示(儘管我認為它與此不一定相關):
#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 <string>
using namespace std;
#define DAEMON_NAME "mydaemon"
void process(){
syslog (LOG_NOTICE, "Writing to log from Daemon");
}
int main(int argc, char *argv[]) {
//Set our Logging Mask and open the Log
setlogmask(LOG_UPTO(LOG_NOTICE));
openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
pid_t pid, sid;
//Fork the Parent Process
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
//We got a good pid, Close the Parent Process
if (pid > 0) { exit(EXIT_SUCCESS); }
//Change File Mask
umask(0);
//Create a new Signature Id for our child
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE); }
// Change to root
chdir("/");
//Close File Descriptors
int x;
for (x = sysconf(_SC_OPEN_MAX); x>=0; x--)
{
close (x);
}
//----------------
//Main Process
//----------------
while(true){
process(); //Run our Process
sleep(30); //Sleep for 30 seconds
break;
}
//Close the log
closelog ();
return 0;
}