Systemctlデーモンはverboseが有効になっている場合にのみ機能します。

Systemctlデーモンはverboseが有効になっている場合にのみ機能します。

init.dにデーモンがあります。構造は、名前と説明を除いて、標準のUbuntuとまったく同じです。スケルトンファイル。私が使用してそのデーモンを実行しようとしたとき

sudo /etc/init.d/mydaemon start

次のメッセージでデーモンの起動に失敗したというエラーメッセージが表示されます。

Control process exited, code=exited, status=1/FAILURE

私が知っている限り、コード1は実際には理解されていないので、これはあまり役に立ちません。このプロセスのデバッグ中に、ある時点で私は出力を送信するために/lib/init/vars.shのverbose変数を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;
}

ベストアンサー1

おすすめ記事