The means to execute a program.
Use the staticstart andrun methods to start a new process.The run method executes the process non-interactively to completion.In contrast, the start method allows your code to interact with therunning process.
Start a process with the run method
The following code sample uses the run method to create a processthat runs the UNIX commandls, which lists the contents of a directory.The run method completes with aProcessResult object when the processterminates. This provides access to the output and exit code from theprocess. The run method does not return aProcess object;this prevents your code from interacting with the running process.
import 'dart:io';main() async { // List all files in the current directory in UNIX-like systems. var result = await Process.run('ls', ['-l']); print(result.stdout);}Start a process with the start method
The following example uses start to create the process.The start method returns aFuture for aProcess object.When the future completes the process is started andyour code can interact with the process:writing to stdin, listening to stdout, and so on.
The following sample starts the UNIXcat utility, which when given nocommand-line arguments, echos its input.The program writes to the process's standard input streamand prints data from its standard output stream.
import 'dart:io';import 'dart:convert';main() async { var process = await Process.start('cat', []); process.stdout .transform(utf8.decoder) .forEach(print); process.stdin.writeln('Hello, world!'); process.stdin.writeln('Hello, galaxy!'); process.stdin.writeln('Hello, universe!');}Standard I/O streams
As seen in the previous code sample, you can interact with theProcess'sstandard output stream through the getterstdout,and you can interact with theProcess's standard input stream throughthe getterstdin.In addition,Process provides a getterstderr for using theProcess'sstandard error stream.
AProcess's streams are distinct from the top-level streamsfor the current program.
NOTE:stdin,stdout, andstderr are implemented using pipes betweenthe parent process and the spawned subprocess. These pipes have limitedcapacity. If the subprocess writes to stderr or stdout in excess of thatlimit without the output being read, the subprocess blocks waiting forthe pipe buffer to accept more data. For example:
import 'dart:io';main() async { var process = await Process.start('cat', ['largefile.txt']); // The following await statement will never complete because the // subprocess never exits since it is blocked waiting for its // stdout to be read. await process.stderr.forEach(print);}Exit codes
Call theexitCode method to get the exit code of the process.The exit code indicates whether the program terminated successfully(usually indicated with an exit code of 0) or with an error.
If the start method is used, theexitCode is available through a futureon theProcess object (as shown in the example below).If the run method is used, theexitCode is availablethrough a getter on theProcessResult instance.
import 'dart:io';main() async { var process = await Process.start('ls', ['-l']); var exitCode = await process.exitCode; print('exit code: $exitCode');}Properties
- exitCode→Future<
int> - A
Futurewhich completes with the exit code of the processwhen the process completes.no setter - hashCode→int
- The hash code for this object.no setterinherited
- pid→int
- The process id of the process.no setter
- runtimeType→Type
- A representation of the runtime type of the object.no setterinherited
- stderr→Stream<
List< int> > - The standard error stream of the process as a
Stream.no setter - stdin→IOSink
- The standard input stream of the process as anIOSink.no setter
- stdout→Stream<
List< int> > - The standard output stream of the process as a
Stream.no setter
Methods
- kill(
[ProcessSignalsignal =ProcessSignal.sigterm])→bool - Kills the process.
- noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- toString(
)→String - A string representation of this object.inherited
Operators
- operator ==(
Objectother)→bool - The equality operator.inherited
Static Methods
- killPid(
intpid, [ProcessSignalsignal =ProcessSignal.sigterm])→bool - Kills the process with id
pid. - run(
Stringexecutable,List< String> arguments, {String?workingDirectory,Map<String,String> ?environment,boolincludeParentEnvironment =true,boolrunInShell =false,Encoding?stdoutEncoding =systemEncoding,Encoding?stderrEncoding =systemEncoding})→Future<ProcessResult> - Starts a process and runs it non-interactively to completion. Theprocess run is
executablewith the specifiedarguments. - runSync(
Stringexecutable,List< String> arguments, {String?workingDirectory,Map<String,String> ?environment,boolincludeParentEnvironment =true,boolrunInShell =false,Encoding?stdoutEncoding =systemEncoding,Encoding?stderrEncoding =systemEncoding})→ProcessResult - Starts a process and runs it to completion. This is a synchronouscall and will block until the child process terminates.
- start(
Stringexecutable,List< String> arguments, {String?workingDirectory,Map<String,String> ?environment,boolincludeParentEnvironment =true,boolrunInShell =false,ProcessStartModemode =ProcessStartMode.normal})→Future<Process> - Starts a process running the
executablewith the specifiedarguments.