systemdは管理されたプロセスの子プロセスの終了をどのように処理しますか?

systemdは管理されたプロセスの子プロセスの終了をどのように処理しますか?

systemd管理プロセスの子プロセス出口を処理する方法は?

systemdデーモンが起動したfoo後、デーモンが3つの異なるデーモン(bar1bar2および)を起動するとしますbar3。予期しないシャットダウンが発生した場合はsystemdどうなりますか?私が理解したのは、プロパティを変更して通知しないと、Solarisのサービス管理機能(SMF)がシャットダウンまたは再開されます。行動に違いはありますか?foobar2foostartdignore_errorsystemd

編集#1:

systemd動作をテストするためにテストデーモンを作成しました。このデーモンはmother_daemon子プロセスを作成するために呼び出されます。

#include <iostream>
#include <unistd.h>
#include <string>
#include <cstring>

using namespace std;

int main(int argc, char* argv[])
{
  cout << "Hi! I'm going to fork and make 5 child processes!" << endl;

  for (int i = 0; i < 5; i++)
    {
    pid_t pid = fork();

    if (pid > 0)
      {
    cout << "I'm the parent process, and i = " << i << endl;
      }
    if (pid == 0)
      {
      // The following four lines rename the process to make it easier to keep track of with ps
    int argv0size = strlen(argv[0]);
    string childThreadName = "mother_daemon child thread PID: ";
    childThreadName.append( to_string(::getpid()) );
    strncpy(argv[0],childThreadName.c_str(),argv0size + 25);

    cout << "I'm a child process, and i = " << i << endl;
    pause();
    // I don't want each child process spawning its own process
    break;
      }
    }
  pause();
  return 0;
  }

これはsystemd次のデバイスによって制御されますmother_daemon.service

[Unit]
Description=Testing how systemd handles the death of the children of a managed process
StopWhenUnneeded=true

[Service]
ExecStart=/home/my_user/test_program/mother_daemon
Restart=always

デバイスmother_daemon.serviceは次のように制御されますmother_daemon.target

[Unit]
Description=A target that wants mother_daemon.service
Wants=mother_daemon.service

実行するとsudo systemctl start mother_daemon.target(後でsudo systemctl daemon-reload)親デーモンと5つの子デーモンを見ることができます。

子のいずれかを殺しても親には影響はありませんが、親を殺すと(したがって再開が始まる)、子が再開されます。

mother_daemon.target止まると子供たちも終わりますsudo systemctl stop mother_daemon.target

これが私の質問に対する答えだと思います。

ベストアンサー1

しかし、実際にはそうではありません。

基本プロセスは、通常の方法で子プロセスの終了を処理します。

これがPOSIXの世界です。プロセスAがBを分岐し、プロセスBがC、D、およびEを分岐すると、プロセスBは終了後にC、D、およびEの状態を確認します。プロセスAはC、D、Eに何が起こったのかわからず、systemdとは何の関係もありませんSIGCHLDwait()

AがC、D、Eが終了したことを知るには、2つのことが必要です。

kevent()(人々はBSDについて賢明になるかもしれませんが、これはLinuxの問題です。)

おすすめ記事