start static method
Starts a process running the executable
with the specified
arguments
.
Returns a Future<Process>
that completes with a
Process instance when the process has been successfully
started. That Process object can be used to interact with the
process. If the process cannot be started the returned Future
completes with an exception.
Using an absolute path for executable
is recommended since resolving
the executable
path is platform-specific. On Windows, both any PATH
set in the environment
map parameter and the path set in
workingDirectory
parameter are ignored for the purposes of resolving
the executable
path.
Use workingDirectory
to set the working directory for the process. Note
that the change of directory occurs before executing the process on some
platforms, which may have impact when using relative paths for the
executable and the arguments.
Use environment
to set the environment variables for the process. If not
set the environment of the parent process is inherited. Currently, only
US-ASCII environment variables are supported and errors are likely to occur
if an environment variable with code-points outside the US-ASCII range is
passed in.
If includeParentEnvironment
is true
, the process's environment will
include the parent process's environment, with environment
taking
precedence. Default is true
.
If runInShell
is true
, the process will be spawned through a system
shell. On Linux and OS X, /bin/sh
is used, while
%WINDIR%\system32\cmd.exe
is used on Windows.
NOTE: On Windows, if executable
is a batch file
('.bat' or '.cmd'), it may be launched by the operating system in a
system shell regardless of the value of runInShell
. This could result in
arguments being parsed according to shell rules. For example:
void main() async {
// Will launch notepad.
Process.start('test.bat', ['test¬epad.exe']);
}
Users must read all data coming on the stdout and stderr
streams of processes started with Process.start
. If the user
does not read all data on the streams the underlying system
resources will not be released since there is still pending data.
The following code uses Process.start
to grep for main
in the
file test.dart
on Linux.
var process = await Process.start('grep', ['-i', 'main', 'test.dart']);
stdout.addStream(process.stdout);
stderr.addStream(process.stderr);
If mode
is ProcessStartMode.normal (the default) a child
process will be started with stdin
, stdout
and stderr
connected to its parent. The parent process will not exit so long as the
child is running, unless exit is called by the parent. If exit is
called by the parent then the parent will be terminated but the child
will continue running.
If mode
is ProcessStartMode.detached a detached process will
be created. A detached process has no connection to its parent,
and can keep running on its own when the parent dies. The only
information available from a detached process is its pid
. There
is no connection to its stdin
, stdout
or stderr
, nor will
the process' exit code become available when it terminates.
If mode
is ProcessStartMode.detachedWithStdio a detached
process will be created where the stdin
, stdout
and stderr
are connected. The creator can communicate with the child through
these. The detached process will keep running even if these
communication channels are closed or the parent dies. The process'
exit code will not become available when it terminated.
The default value for mode
is ProcessStartMode.normal
.
Implementation
external static Future<Process> start(
String executable,
List<String> arguments, {
String? workingDirectory,
Map<String, String>? environment,
bool includeParentEnvironment = true,
bool runInShell = false,
ProcessStartMode mode = ProcessStartMode.normal,
});