All Projects → pgreze → kotlin-process

pgreze / kotlin-process

Licence: Apache-2.0 license
Kotlin friendly way to run an external process

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to kotlin-process

script.kt
Convenient kotlin script wrapper
Stars: ✭ 15 (-81.25%)
Mutual labels:  kotlin-script, kotlin-scripting
KotlinW
A small wrapper for the Kotlin compiler that can be used to execute .kts scripts
Stars: ✭ 18 (-77.5%)
Mutual labels:  kotlin-script
Multiprocess
🚀Easy to make the common PHP/Python/js...script change daemon and multi-process execution
Stars: ✭ 151 (+88.75%)
Mutual labels:  process
Processloadingview
ProcessLoadingView is a step indicator loading animation built using CABasicAnimation
Stars: ✭ 251 (+213.75%)
Mutual labels:  process
Invoker
Penetration testing utility, and antivirus assessment tool.
Stars: ✭ 178 (+122.5%)
Mutual labels:  process
dodoc
Conçu pour favoriser les processus réflexifs lors d’activités d’apprentissage, do•doc est un outil ouvert et modulaire qui permet de capturer des médias (photos, vidéos, sons et stop-motion), de les éditer, de les mettre en page et de les publier
Stars: ✭ 51 (-36.25%)
Mutual labels:  process
Procs
A modern replacement for ps written in Rust
Stars: ✭ 2,435 (+2943.75%)
Mutual labels:  process
doc
Get usage and health data about your Node.js process.
Stars: ✭ 17 (-78.75%)
Mutual labels:  process
pagent
child process agent by golang
Stars: ✭ 18 (-77.5%)
Mutual labels:  process
W7 Rangine Empty
软擎是基于 Php 7.2+ 和 Swoole 4.4+ 的高性能、简单易用的开发框架。支持同时在 Swoole Server 和 php-fpm 两种模式下运行。内置了 Http (Swoole, Fpm),Tcp,WebSocket,Process,Crontab服务。集成了大量成熟的组件,可以用于构建高性能的Web系统、API、中间件、基础服务等等。
Stars: ✭ 246 (+207.5%)
Mutual labels:  process
Human Signals
Human-friendly process signals
Stars: ✭ 223 (+178.75%)
Mutual labels:  process
Compileflow
core business process engine of Alibaba Halo platform. best engine for trade Scenes
Stars: ✭ 179 (+123.75%)
Mutual labels:  process
codee-app
Android IDE for programming fully written on Kotlin
Stars: ✭ 42 (-47.5%)
Mutual labels:  kotlin-script
Mandibule
linux elf injector for x86 x86_64 arm arm64
Stars: ✭ 171 (+113.75%)
Mutual labels:  process
HowLong
A simple command line application that lets you know how long your command has been running.
Stars: ✭ 17 (-78.75%)
Mutual labels:  process
Kill Port
❌ Kill the process running on given port
Stars: ✭ 143 (+78.75%)
Mutual labels:  process
Htop
htop - an interactive process viewer
Stars: ✭ 3,076 (+3745%)
Mutual labels:  process
libmem
Advanced Game Hacking Library for C/C++, Rust and Python (Windows/Linux/FreeBSD) (Process/Memory Hacking) (Hooking/Detouring) (Cross Platform) (x86/x64/ARM/ARM64) (DLL/SO Injection) (Internal/External)
Stars: ✭ 336 (+320%)
Mutual labels:  process
moneta
Moneta is a live usermode memory analysis tool for Windows with the capability to detect malware IOCs
Stars: ✭ 384 (+380%)
Mutual labels:  process
acikseminer2020
servis ve süreçlerle ilgili basit örnekler
Stars: ✭ 41 (-48.75%)
Mutual labels:  process

kotlin-process License Build codecov

Functional Kotlin friendly way to create external system processes by leveraging:

Installation central

repositories {
    mavenCentral()
}

dependencies {
    // Check the 🔝 maven central badge 🔝 for the latest $kotlinProcessVersion
    implementation("com.github.pgreze:kotlin-process:$kotlinProcessVersion")
}

Or in your kotlin script:

@file:DependsOn("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
@file:DependsOn("com.github.pgreze:kotlin-process:$kotlinProcessVersion")

Usage

Launch a script and consume its results

Starts a program and prints its stdout/stderr outputs to the terminal:

import com.github.pgreze.process.process
import kotlinx.coroutines.runBlocking

runBlocking {
    val res = process("echo", "hello world")

    check(res.resultCode == 0)

    // By default process output is displayed in the console.
    check(res.output == emptyList<String>())
}

The next step would probably be to capture the output stream, in order to process some data from our own-made script:

val output = process(
    "./my-script.sh", arg1, arg2,

    // Capture stdout lines to do some operations after
    stdout = Redirect.CAPTURE,

    // Default value: prints to System.err
    stderr = Redirect.PRINT,

).unwrap() // Fails if the resultCode != 0

// TODO: process the output
println("Success:\n${output.joinToString("\n")}")

Notice that if you want to capture both stdout and stderr, there will be no way to differentiate them in the returned output:

val res = process(
    "./long-and-dangerous.sh", arg1, arg2,

    // Both streams will be captured,
    // preserving their orders but mixing them in the given output.
    stdout = Redirect.CAPTURE,
    stderr = Redirect.CAPTURE,

    // Allows to consume line by line without delay the provided output.
    consumer = { line -> TODO("process $line") },
)

println("Script finished with result=${res.resultCode}")
println("stdout+stderr:\n" + res.output.joinToString("\n"))

It's also possible to redirect an output stream to a file, or manually by consuming a Flow instance.

import com.github.pgreze.process.Redirect
import com.github.pgreze.process.process
import java.io.File
import java.util.Collections
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking

val errLines = Collections.synchronizedList(mutableListOf<String>())
val res = process(
    "./my-script.sh", arg1, arg2,

    // You can save the execution result in a file,
    stdout = Redirect.ToFile(File("my-input.txt")),

    // If you want to handle this stream yourself,
    // a Flow<String> instance can be used.
    stderr = Redirect.Consume { flow -> flow.toList(errLines) },
)

The last but not least, you can just silence a stream with Redirect.SILENT 😶

Control the environment

Several other options are available to control the script environment:

import com.github.pgreze.process.InputSource
import java.io.File

val res = process(
    "./my-script.sh",
    
    // Provides the input as a string, similar to:
    // $ echo "hello world" | my-script.sh
    stdin = InputSource.fromString("hello world"),

    // Inject custom environment variables:
    env = mapOf("MY_ENV_VARIABLE" to "42"),

    // Override the working directory:
    directory = File("./a/directory"),
)

There are other ways to provide the process input:

// From a file:
process(
    "./my-script.sh",
    stdin = InputSource.FromFile(File("my-input.txt")),
)

// From an InputStream:
process(
    "./my-script.sh",
    stdin = InputSource.fromInputStream(myInputStream)),
)

// Manually by using the raw OutputStream:
process(
    "./my-script.sh",
    stdin = InputSource.FromStream { out: OutputStream ->
        out.write("hello world\n".toByteArray())
        out.flush()
    },
)

Alternative(s)

  1. https://github.com/jakubriegel/kotlin-shell
  2. https://github.com/lordcodes/turtle
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].