8 releases
0.4.2 | Nov 8, 2024 |
---|---|
0.4.1 | Nov 6, 2024 |
0.3.0 | Oct 23, 2024 |
0.2.10 | Oct 15, 2024 |
0.2.8 | Aug 25, 2024 |
#507 in Unix APIs
24 downloads per month
44KB
887 lines
pipelight_exec crate
Internal crate from pipelight.
lib.rs
:
Warning - Unstable crate.
This crate is still in development and undergoing API changes.
It is internally used in the pipelight cicd engine: https://github.com/pipelight/pipelight
Breanking changes
- v0.4 : New API for easier process manipulation. p.run_detached() becomes p.detach().run();
About
A crate for easy process management (on Unix systems).
It makes a best effort to leverage the standard library process crate.
Features:
-
Get process execution time.
-
Spawn and Kill background processes.
-
Display a running process standard inputs/outputs.
Usage
Spawn a process
Spawn a simple process in the background. or in other words, execute a process and detach it.
It keeps running after parent process exit and terminal exit.
// Runs a child process and wait until execution is over.
let mut p = Process::new()
.stdin("echo test")
.to_owned();
p.run()?;
Runs child process in the background. Do not wait until process is over and return as soon as child is spawned.
let mut p = Process::new()
.stdin("echo test")
.background()
.to_owned();
p.run()?;
Runs a disowned child process that won't be killed if parent is killed. Do not wait until process is over and return as soon as child is spawned.
let mut p = Process::new()
.stdin("echo test")
.detach()
.to_owned();
p.run()?;
Runs a disowned child process that won't be killed if parent is killed. and stores process outputs in ./.pipelight/proc/:uuid/ Practical if you want to process child output from another program.
let mut p = Process::new()
.stdin("echo test")
.fs()
.detach()
.to_owned();
p.run()?;
Read a process i/o.
let mut p = Process::new()
.stdin("echo test")
.background()
.fs()
.to_owned();
p.run()?;
// Later in execution
p.io.read().into_diagnostic()?;
println!("{:?}", p.io.stdout); // Some("stuff\n")
Find a process.
Find a running process, with handy search options.
let process_finder = Finder::new().seed("my_proc").root("/my/dir").search()?;
let pid = 1792;
let process_finder = Finder::new().pid(&pid).search()?;
Beware - Unconventional process management
In linux, a process inputs and outputs are exposed at /proc/:process_id/fd/:fd_id and are deleted as soon as the process finishes its execution.
A very good straightforward rustacean overview at procfs crate
To keep track of running and dead processes, this crate can redirect some process i/o into its own managed files. Pipelight managed processes are stored in a the .pipelight/proc/ directory. It has the same structure as your /proc/.
However, to ease ones developer life, standard outputs are polled, read and finally stored as text files. /proc/:pid/2 (file descriptor / buffer)-> ./pipelight/proc/:uuid/2(text file)
For example,
The following code runs a process whose outputs are redirected
to pipelight temporary filesystem thanks to the fs()
method.
let p = Process::new().stdin("pwd").fs().run()?;
p.io.read().into_diagnostic()?;
let stdout: Option<String> = p.io.stdout;
It allows us to read a process outputs during and long after execution.
Dependencies
~12–22MB
~342K SLoC