Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/PublishIncrementalNuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
jobs:
build:
runs-on: ubuntu-latest
permissions:
id-token: write # trusted publishing: OIDC token exchanged for a NuGet API key
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -33,11 +36,17 @@ jobs:
run: |
dotnet pack --no-build CShell.sln -c Release -o packages --include-symbols --property WarningLevel=0

- name: NuGet login (trusted publishing)
id: nuget-login
uses: NuGet/login@v1
with:
user: thermous

- name: Publish NuGet and symbols
id: nuget-push
uses: edumserrano/nuget-push@v1
with:
api-key: '${{ secrets.NUGET_KEY }}'
api-key: '${{ steps.nuget-login.outputs.NUGET_API_KEY }}'
working-directory: 'packages'
fail-if-exists: false

Expand Down
11 changes: 10 additions & 1 deletion .github/workflows/PublishNuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
jobs:
build:
runs-on: windows-latest
permissions:
id-token: write # trusted publishing: OIDC token exchanged for a NuGet API key
contents: read
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -32,11 +35,17 @@ jobs:
# run: |
# nuget pack template/CShell.Template/CShell.Template.nuspec -OutputDirectory packages

- name: NuGet login (trusted publishing)
id: nuget-login
uses: NuGet/login@v1
with:
user: thermous

- name: Publish NuGet and symbols
id: nuget-push
uses: edumserrano/nuget-push@v1
with:
api-key: '${{ secrets.NUGET_KEY }}'
api-key: '${{ steps.nuget-login.outputs.NUGET_API_KEY }}'
working-directory: 'packages'
fail-if-exists: false

11 changes: 10 additions & 1 deletion .github/workflows/PublishTemplates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
jobs:
build:
runs-on: windows-latest
permissions:
id-token: write # trusted publishing: OIDC token exchanged for a NuGet API key
contents: read
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -17,11 +20,17 @@ jobs:
run: |
nuget pack template/CShell.Template/CShell.Template.nuspec -OutputDirectory packages

- name: NuGet login (trusted publishing)
id: nuget-login
uses: NuGet/login@v1
with:
user: thermous

- name: Publish NuGet and symbols
id: nuget-push
uses: edumserrano/nuget-push@v1
with:
api-key: '${{ secrets.NUGET_KEY }}'
api-key: '${{ steps.nuget-login.outputs.NUGET_API_KEY }}'
working-directory: 'packages'
fail-if-exists: false

49 changes: 48 additions & 1 deletion CShell.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 18
VisualStudioVersion = 18.6.11822.322 stable
VisualStudioVersion = 18.6.11822.322
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CShell", "src\CShell.csproj", "{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}"
EndProject
Expand All @@ -18,28 +18,75 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "Sample\Sample.csproj", "{945CEB6E-0534-45AE-A63E-AE8F08A850CC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BackgroundHost", "Tests\BackgroundHost\BackgroundHost.csproj", "{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Debug|x64.ActiveCfg = Debug|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Debug|x64.Build.0 = Debug|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Debug|x86.ActiveCfg = Debug|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Debug|x86.Build.0 = Debug|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Release|Any CPU.Build.0 = Release|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Release|x64.ActiveCfg = Release|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Release|x64.Build.0 = Release|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Release|x86.ActiveCfg = Release|Any CPU
{9705E191-6CF0-4F55-B3E2-C34A81A7FE28}.Release|x86.Build.0 = Release|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Debug|x64.ActiveCfg = Debug|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Debug|x64.Build.0 = Debug|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Debug|x86.ActiveCfg = Debug|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Debug|x86.Build.0 = Debug|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Release|Any CPU.Build.0 = Release|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Release|x64.ActiveCfg = Release|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Release|x64.Build.0 = Release|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Release|x86.ActiveCfg = Release|Any CPU
{3EC7173A-D8DB-4CFB-B63E-CBE688AD5C3D}.Release|x86.Build.0 = Release|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Debug|x64.ActiveCfg = Debug|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Debug|x64.Build.0 = Debug|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Debug|x86.ActiveCfg = Debug|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Debug|x86.Build.0 = Debug|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Release|Any CPU.Build.0 = Release|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Release|x64.ActiveCfg = Release|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Release|x64.Build.0 = Release|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Release|x86.ActiveCfg = Release|Any CPU
{945CEB6E-0534-45AE-A63E-AE8F08A850CC}.Release|x86.Build.0 = Release|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Debug|x64.ActiveCfg = Debug|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Debug|x64.Build.0 = Debug|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Debug|x86.ActiveCfg = Debug|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Debug|x86.Build.0 = Debug|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Release|Any CPU.Build.0 = Release|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Release|x64.ActiveCfg = Release|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Release|x64.Build.0 = Release|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Release|x86.ActiveCfg = Release|Any CPU
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D7C0AF0C-16BB-4FFF-9706-377A3395FC47} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F21757E8-6291-4BCC-AAF7-C9C0B805D67C}
EndGlobalSection
Expand Down
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,18 @@ to make it even easier to work with the output of processes.
| **Run(program, arg1, ..., argN)** | run a program and CAPTURE its output, for reading and piping |
| **Exec()/ExecAsync(program, arg1, ..., argN)** | run a program ATTACHED to this console, returning its exit code |
| **Start(program, arg1, ..., argN)** | run a DETACHED program directly with the given args (aka Process.Start(program, args)|
| **Background(program, arg1, ..., argN)** | run a program in the BACKGROUND, sharing nothing with this one, returning its process id |
| **Cmd(cmd)** | run the cmd inside a cmd.exe, allow you to execute shell commands (like dir /b *.* |
| **Bash(bash)** | run the program in bash environment, allow you to execute bash shell commands (like ls -al * |

Three ways to run something, and the difference is where its input and output go:
Four ways to run something, and the difference is where its input and output go:

| | Output | Use it for |
|---|---|---|
| **Run()** | captured | anything you want to read, pipe, or parse |
| **Exec()** | your console | anything the user has to see and answer |
| **Start()**| its own window | anything you are not waiting for |
| **Start()**| its own window | apps you open and leave |
| **Background()** | nowhere | servers and daemons that outlive the script |

`Exec()` is what a shell does by default. In bash, `ssh host` or `vim` takes over the terminal and
you opt into capture with `$(...)`; `Run()` is the other way round, so anything that needs the
Expand All @@ -297,6 +299,31 @@ stdin pipe nothing will write to.
Terminal settings are restored afterwards, so a program killed before it can tidy up does not leave
your console with echo switched off.

`Background()` is `program &` with nohup: it returns at once with the process id, and the program
carries on after the script has finished.

```CSharp
var pid = Background("dotnet", "MyServer.dll", "--port", "8080");

Background(opt => opt.WorkingDirectory(site)
.EnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"),
"dotnet", "run");
```

It shares nothing with the script that started it:

* **No window**, and no console you can see.
* **None of the script's streams or handles.** A process holding a copy of the script's stdout keeps
whoever is reading it - `$(script)`, a pipe, a CI step, a test harness - waiting until it exits,
which for a server is never. Start() and Run() cannot promise this; Background() can. Its own
output goes nowhere, so have it write a log file if it has something to say.
* **Cut loose from the terminal.** On Windows it is in its own process group, so Ctrl+C does not
reach it; on Linux it is in its own session, so neither Ctrl+C nor closing the terminal does.
macOS has no `setsid`, so there it runs under `nohup`: it survives the terminal closing, but not a
Ctrl+C sent while the script is still running.

The id is the program's own, for `Process.GetProcessById()` when you want to watch or stop it.

```CSharp
// Invoke multiple commands using fluent style
var cmd1= await Run("cmd1", "args1")
Expand Down
14 changes: 14 additions & 0 deletions Tests/BackgroundHost/BackgroundHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<!-- A helper the Background() tests run as a separate process. -->
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\CShell.csproj" />
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions Tests/BackgroundHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// A helper for the Background() tests, run as its own process:
//
// report <file> [args...] writes what it was started with to <file> and exits:
// pid, working folder, CSHELL_BACKGROUND_TEST, Linux session id, args
// launch starts `sleep 60` of itself with Background(), prints the id it
// got back, and exits at once
// sleep <seconds> sleeps

using System;
using System.IO;
using System.Linq;
using System.Threading;
using CShellNet;

switch (args[0])
{
case "report":
var lines = new[]
{
Environment.ProcessId.ToString(),
Environment.CurrentDirectory,
Environment.GetEnvironmentVariable("CSHELL_BACKGROUND_TEST") ?? "<unset>",
LinuxSession("self"),
}.Concat(args.Skip(2));
// Written whole and then moved, so the test never reads half a file.
File.WriteAllLines(args[1] + ".tmp", lines);
File.Move(args[1] + ".tmp", args[1]);
return 0;

case "launch":
var self = typeof(Program).Assembly.Location;
var shell = new CShell { Echo = false };
Console.WriteLine(shell.Background(Environment.ProcessPath, self, "sleep", "60"));
return 0;

case "sleep":
Thread.Sleep(TimeSpan.FromSeconds(int.Parse(args[1])));
return 0;

default:
return 2;
}

// The session id from /proc/<pid>/stat: the fourth field after the parenthesised name.
static string LinuxSession(string pid)
{
var stat = $"/proc/{pid}/stat";
if (!File.Exists(stat))
{
return "";
}

var text = File.ReadAllText(stat);
return text.Substring(text.LastIndexOf(')') + 2).Split(' ')[3];
}
Loading
Loading