{"id":26030061,"url":"https://github.com/asmichi/childprocess","last_synced_at":"2025-04-07T18:13:22.192Z","repository":{"id":131591099,"uuid":"184371448","full_name":"asmichi/ChildProcess","owner":"asmichi","description":"Asmichi.ChildProcess is a .NET library that provides functionality for creating child processes. An alternative to `System.Diagnostics.Process.Start`.","archived":false,"fork":false,"pushed_at":"2024-10-29T15:52:29.000Z","size":490,"stargazers_count":35,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-07T18:13:16.435Z","etag":null,"topics":["child-process","dotnet","dotnet-core","linux","process","windows"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asmichi.png","metadata":{"files":{"readme":"README.ja.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-01T05:05:36.000Z","updated_at":"2025-03-11T02:24:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"d6a701a5-b53e-475d-b3db-158d13e4c91f","html_url":"https://github.com/asmichi/ChildProcess","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmichi%2FChildProcess","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmichi%2FChildProcess/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmichi%2FChildProcess/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmichi%2FChildProcess/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asmichi","download_url":"https://codeload.github.com/asmichi/ChildProcess/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247704571,"owners_count":20982298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["child-process","dotnet","dotnet-core","linux","process","windows"],"created_at":"2025-03-06T18:19:45.625Z","updated_at":"2025-04-07T18:13:22.171Z","avatar_url":"https://github.com/asmichi.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"[English](README.md)\n\n# Asmichi.ChildProcess\n子プロセスを生成する機能を提供します。子プロセスを生成して操作することに関しては、`System.Diagnostics.Process` よりも簡単に使えます。また、デフォルト値がより安全であり、柔軟なリダイレクト制御も可能です。\n\n[NuGet](https://www.nuget.org/packages/Asmichi.ChildProcess/) で取得することができます。\n\n[![Build Status](https://dev.azure.com/asmichi/ChildProcess/_apis/build/status/ChildProcess-CI?branchName=master)](https://dev.azure.com/asmichi/ChildProcess/_build/latest?definitionId=5\u0026branchName=master)\n\n[Wiki](https://github.com/asmichi/ChildProcess/wiki) に本ライブラリのゴールとロードマップがあります。\n\n## `System.Diagnostics.Process` との比較\n\n- 子プロセスを生成してその出力を得ることに注力している。\n    - プロセスの状態を取得することはできない。\n    - 常駐プロセスを生成することはできない。\n- より多くのリダイレクト先をサポート:\n    - NUL\n    - ファイル (追記も可能)\n    - パイプ\n    - ハンドル\n- リダイレクトのデフォルト値がより安全:\n    - stdin は NUL\n    - stdout は現在の stdout\n    - stderr は現在の stderr\n- パイプは非同期である。すなわち、非同期の読み書きは IO 完了ポートが扱う。\n- 子プロセスの終了を保証する。\n\n# ライセンス\n\n[The MIT License](LICENSE)\n\n# サポートされるランタイム\n\n- .NET Core 3.1 以降\n\nRIDs:\n\n- `win10-x86` (未テスト)\n- `win10-x64` (1809 以降; 1809 でテスト済)\n- `win10-arm` (未テスト)\n- `win10-arm64` (未テスト)\n- `linux-x64` (Ubuntu 18.04 でテスト済)\n- `linux-arm` (未テスト)\n- `linux-arm64` (未テスト)\n- `linux-musl-arm64` (未テスト)\n- `linux-musl-x64` (Alpine 3.20 でテスト済)\n- `osx-x64` (macOS 10.15 Catalina 以降; 12 でテスト済)\n- `osx-arm64` (macOS 11.0 Big Sur 以降; 未テスト)\n\nNOTE: glibc ベースの Linux では、glibc 2.27 以降と libstdc++ 3.4.25 以降が必要です。\n\n# Known Issues\n\n- Windows 11 24H2 より前の Windows において、 `ChildProcess.Start` 実行は、 `conhost.exe` へのプロセスハンドルを 1 つリークします。(`ChildProcessFlags.AttachToCurrentConsole` により子プロセスを現在のコンソールにアタッチできた場合を除く。).\n    - [Kernel32 の不具合](https://github.com/microsoft/terminal/issues/17903) に起因します。\n- Windows 10 1809 (Windows Server 2019 を含む) では、 `SignalTermination` は単にプロセスツリーを強制終了します (`Kill` と同じ操作です).\n    - [`ClosePseudoConsole`](https://docs.microsoft.com/en-us/windows/console/closepseudoconsole) が pseudoconsole にアタッチされたプログラムを終了しないバグがあるためです。\n- 11.0 より前の macOS では、シグナルによって終了したプロセスの `ExitCode` は常に `-1` になります。\n    - そのようなプロセスについて `waitid` が `siginfo_t.si_status` に `0` を返すバグがあるためです。\n\n# 注意\n\n- `ChildProcessCreationContext` や `ChildProcessFlags. DisableEnvironmentVariableInheritance` を使用して環境変数を完全に上書きする場合、 `SystemRoot` などの基本的な環境変数を含めることを推奨します。\n\n# 制限事項\n\n- 最大で 2^63 個のプロセスしか生成できません。\n\n# ランタイムに関する仮定\n\n本ライブラリは、ランタイムに関して以下の性質を仮定しています:\n\n- Windows\n    - `SafeFileHandle` の内部値はファイルハンドルである。\n    - `SafeWaitHandle` の内部値は `WaitForSingleObject` で待つことができる。\n    - `SafeProcessHandle` の内部値はプロセスハンドルである。\n- *nix\n    - `SafeFileHandle` の内部値はファイルディスクリプタである。\n    - `SafeProcessHandle` の内部値はプロセス ID である。\n    - `Socket.Handle` はソケットのファイルディスクリプタを返す。\n\n# サンプル\n\n追加のサンプルは [ChildProcessExample.sln](ChildProcessExample.sln) を開くか、[ChildProcess.Example](src/ChildProcess.Example/) を参照してください。\n\n## 基本\n\n子プロセスの出力を読み取ることができます。その際、 stdout と stderr を結合することもできます。\n\n```cs\nvar si = new ChildProcessStartInfo(\"cmd\", \"/C\", \"echo\", \"foo\")\n{\n    StdOutputRedirection = OutputRedirection.OutputPipe,\n    // 2\u003e\u00261 のような効果\n    StdErrorRedirection = OutputRedirection.OutputPipe,\n    // DisableArgumentQuoting: See ChildProcessExamplesWindows.cs for details\n    Flags = ChildProcessFlags.DisableArgumentQuoting,\n};\n\nusing (var p = ChildProcess.Start(si))\n{\n    using (var sr = new StreamReader(p.StandardOutput))\n    {\n        // \"foo\"\n        Console.Write(await sr.ReadToEndAsync());\n    }\n    await p.WaitForExitAsync();\n    // ExitCode: 0\n    Console.WriteLine(\"ExitCode: {0}\", p.ExitCode);\n}\n```\n\n## ファイルへのリダイレクト\n\n子プロセスの出力をファイルにリダイレクトできます。その際、子プロセスの出力を読む必要はありません。\n\n```cs\nvar tempFile = Path.GetTempFileName();\n\nvar si = new ChildProcessStartInfo(\"cmd\", \"/C\", \"set\")\n{\n    ExtraEnvironmentVariables = new Dictionary\u003cstring, string\u003e { { \"A\", \"A\" } },\n    StdOutputRedirection = OutputRedirection.File,\n    StdErrorRedirection = OutputRedirection.File,\n    StdOutputFile = tempFile,\n    StdErrorFile = tempFile,\n    // DisableArgumentQuoting: See ChildProcessExamplesWindows.cs for details\n    Flags = ChildProcessFlags.UseCustomCodePage | ChildProcessFlags.DisableArgumentQuoting,\n    CodePage = Encoding.Default.CodePage, // UTF-8 on .NET Core\n};\n\nusing (var p = ChildProcess.Start(si))\n{\n    await p.WaitForExitAsync();\n}\n\n// A=A\n// ALLUSERSPROFILE=C:\\ProgramData\n// ...\nConsole.WriteLine(File.ReadAllText(\"env.txt\"));\nFile.Delete(tempFile);\n```\n\n## 真のパイプ\n\n子プロセスの出力を、ほかの子プロセスの入力にパイプできます。その際、子プロセスの出力を読む必要はありません。\n\n```cs\n// 匿名パイプを作る\nusing var inPipe = new AnonymousPipeServerStream(PipeDirection.In);\n\nvar si1 = new ChildProcessStartInfo(\"cmd\", \"/C\", \"set\")\n{\n    // 出力をパイプの書き込み側に接続する\n    StdOutputRedirection = OutputRedirection.Handle,\n    StdErrorRedirection = OutputRedirection.Handle,\n    StdOutputHandle = inPipe.ClientSafePipeHandle,\n    StdErrorHandle = inPipe.ClientSafePipeHandle,\n    // DisableArgumentQuoting: See ChildProcessExamplesWindows.cs for details\n    Flags = ChildProcessFlags.UseCustomCodePage | ChildProcessFlags.DisableArgumentQuoting,\n    CodePage = Encoding.Default.CodePage, // UTF-8 on .NET Core\n};\n\nvar si2 = new ChildProcessStartInfo(\"findstr\", \"Windows\")\n{\n    // 入力をパイプの読み取り側に接続する\n    StdInputRedirection = InputRedirection.Handle,\n    StdInputHandle = inPipe.SafePipeHandle,\n    StdOutputRedirection = OutputRedirection.OutputPipe,\n    StdErrorRedirection = OutputRedirection.OutputPipe,\n    Flags = ChildProcessFlags.UseCustomCodePage,\n    CodePage = Encoding.Default.CodePage, // UTF-8 on .NET Core\n};\n\nusing var p1 = ChildProcess.Start(si1);\nusing var p2 = ChildProcess.Start(si2);\n\n// パイプハンドルのコピーを閉じる。 (そうしないと、 p2 はパイプからの読み取りでブロックし続ける。)\ninPipe.DisposeLocalCopyOfClientHandle();\ninPipe.Close();\n\nusing (var sr = new StreamReader(p2.StandardOutput))\n{\n    // ...\n    // OS=Windows_NT\n    // ...\n    Console.Write(await sr.ReadToEndAsync());\n}\n\nawait p1.WaitForExitAsync();\nawait p2.WaitForExitAsync();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasmichi%2Fchildprocess","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasmichi%2Fchildprocess","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasmichi%2Fchildprocess/lists"}