A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.deno.com/examples/subprocess_tutorial/ below:

Creating a subprocess

On this page Creating a subprocess Concepts Jump to heading# Simple example Jump to heading#

This example is the equivalent of running echo "Hello from Deno!" from the command line.

subprocess_simple.ts


const command = new Deno.Command("echo", {
  args: [
    "Hello from Deno!",
  ],
});


const { code, stdout, stderr } = await command.output();

console.assert(code === 0);
console.log(new TextDecoder().decode(stdout));
console.log(new TextDecoder().decode(stderr));

Run it:

$ deno run --allow-run=echo ./subprocess_simple.ts
Hello from Deno!
Security Jump to heading#

The --allow-run permission is required for creation of a subprocess. Be aware that subprocesses are not run in a Deno sandbox and therefore have the same permissions as if you were to run the command from the command line yourself.

Communicating with subprocesses Jump to heading#

By default when you use Deno.Command() the subprocess inherits stdin, stdout and stderr of the parent process. If you want to communicate with a started subprocess you must use the "piped" option.

Piping to files Jump to heading#

This example is the equivalent of running yes &> ./process_output in bash.

subprocess_piping_to_files.ts

import {
  mergeReadableStreams,
} from "jsr:@std/streams@1.0.0-rc.4/merge-readable-streams";


const file = await Deno.open("./process_output.txt", {
  read: true,
  write: true,
  create: true,
});


const command = new Deno.Command("yes", {
  stdout: "piped",
  stderr: "piped",
});

const process = command.spawn();


const joined = mergeReadableStreams(
  process.stdout,
  process.stderr,
);


joined.pipeTo(file.writable).then(() => console.log("pipe join done"));


setTimeout(() => {
  process.kill();
}, 100);

Run it:

$ deno run --allow-run=yes --allow-read=. --allow-write=. ./subprocess_piping_to_file.ts
Did you find what you needed?

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4