Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Control.Process.Process
Synopsis
- type Pid = CPid
- withCreateProcess :: CreateProcess -> (Maybe Handle -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a) -> IO a
- terminateProcess :: ProcessHandle -> IO ()
- spawnProcess :: FilePath -> [String] -> IO ProcessHandle
- showCommandForUser :: FilePath -> [String] -> String
- shell :: String -> CreateProcess
- readProcess :: FilePath -> [String] -> String -> IO String
- readCreateProcess :: CreateProcess -> String -> IO String
- proc :: FilePath -> [String] -> CreateProcess
- getPid :: ProcessHandle -> IO (Maybe Pid)
- getCurrentPid :: IO Pid
- createProcess :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
- cleanupProcess :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
- callProcess :: FilePath -> [String] -> IO ()
- callCommand :: String -> IO ()
- interruptProcessGroupOf :: ProcessHandle -> IO ()
- createProcess_ :: String -> CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
- createPipeFd :: IO (FD, FD)
- createPipe :: IO (Handle, Handle)
- data StdStream
- data ProcessHandle
- data CreateProcess
- data CmdSpec
- readCreateProcessWithExitCode :: Exception e' => CreateProcess -> String -> ExitcodeT (ExceptT e' IO) (String, String) (String, String)
- readProcessWithExitCode :: Exception e' => FilePath -> [String] -> String -> ExitcodeT (ExceptT e' IO) (String, String) (String, String)
- waitForProcess :: Exception e' => ProcessHandle -> ExitcodeT (ExceptT e' IO) () ()
- getProcessExitCode :: Exception e' => ProcessHandle -> ExitcodeT (ExceptT e' IO) (Maybe ()) (Maybe ())
- getProcessExitCodeBool :: Exception e' => ProcessHandle -> ExitcodeT (ExceptT e' IO) Bool Bool
Documentation
The platform specific type for a process identifier.
This is always an integral type. Width and signedness are platform specific.
Since: process-1.6.3.0
withCreateProcess :: CreateProcess -> (Maybe Handle -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a) -> IO a #
A bracket
-style resource handler for createProcess
.
Does automatic cleanup when the action finishes. If there is an exception
in the body then it ensures that the process gets terminated and any
CreatePipe
Handle
s are closed. In particular this means that if the
Haskell thread is killed (e.g. killThread
), that the external process is
also terminated.
e.g.
withCreateProcess (proc cmd args) { ... } $ \stdin stdout stderr ph -> do ...
Since: process-1.4.3.0
terminateProcess :: ProcessHandle -> IO () #
Attempts to terminate the specified process. This function should
not be used under normal circumstances - no guarantees are given regarding
how cleanly the process is terminated. To check whether the process
has indeed terminated, use getProcessExitCode
.
On Unix systems, terminateProcess
sends the process the SIGTERM signal.
On Windows systems, if use_process_jobs
is True
then the Win32 TerminateJobObject
function is called to kill all processes associated with the job and passing the
exit code of 1 to each of them. Otherwise if use_process_jobs
is False
then the
Win32 TerminateProcess
function is called, passing an exit code of 1.
Note: on Windows, if the process was a shell command created by
createProcess
with shell
, or created by runCommand
or
runInteractiveCommand
, then terminateProcess
will only
terminate the shell, not the command itself. On Unix systems, both
processes are in a process group and will be terminated together.
spawnProcess :: FilePath -> [String] -> IO ProcessHandle #
Creates a new process to run the specified raw command with the given
arguments. It does not wait for the program to finish, but returns the
ProcessHandle
.
Since: process-1.2.0.0
showCommandForUser :: FilePath -> [String] -> String #
Given a program p
and arguments args
,
showCommandForUser p args
returns a string suitable for pasting
into /bin/sh
(on Unix systems) or CMD.EXE
(on Windows).
shell :: String -> CreateProcess #
Construct a CreateProcess
record for passing to createProcess
,
representing a command to be passed to the shell.
Arguments
:: FilePath | Filename of the executable (see |
-> [String] | any arguments |
-> String | standard input |
-> IO String | stdout |
readProcess
forks an external process, reads its standard output
strictly, blocking until the process terminates, and returns the output
string. The external process inherits the standard error.
If an asynchronous exception is thrown to the thread executing
readProcess
, the forked process will be terminated and readProcess
will
wait (block) until the process has been terminated.
Output is returned strictly, so this is not suitable for launching processes that require interaction over the standard file streams.
This function throws an IOError
if the process ExitCode
is
anything other than ExitSuccess
. If instead you want to get the
ExitCode
then use readProcessWithExitCode
.
Users of this function should compile with -threaded
if they
want other Haskell threads to keep running while waiting on
the result of readProcess.
> readProcess "date" [] [] "Thu Feb 7 10:03:39 PST 2008\n"
The arguments are:
- The command to run, which must be in the $PATH, or an absolute or relative path
- A list of separate command line arguments to the program
- A string to pass on standard input to the forked process.
Arguments
:: CreateProcess | |
-> String | standard input |
-> IO String | stdout |
readCreateProcess
works exactly like readProcess
except that it
lets you pass CreateProcess
giving better flexibility.
> readCreateProcess ((shell "pwd") { cwd = Just "/etc/" }) "" "/etc\n"
Note that Handle
s provided for std_in
or std_out
via the CreateProcess
record will be ignored.
Since: process-1.2.3.0
proc :: FilePath -> [String] -> CreateProcess #
Construct a CreateProcess
record for passing to createProcess
,
representing a raw command with arguments.
See RawCommand
for precise semantics of the specified FilePath
.
getPid :: ProcessHandle -> IO (Maybe Pid) #
Returns the PID (process ID) of a subprocess.
Nothing
is returned if the handle was already closed. Otherwise a
PID is returned that remains valid as long as the handle is open.
The operating system may reuse the PID as soon as the last handle to
the process is closed.
Since: process-1.6.3.0
getCurrentPid :: IO Pid #
Returns the PID (process ID) of the current process. On POSIX systems,
this calls getProcessID
from System.Posix.Process in the unix
package.
On Windows, this calls getCurrentProcessId
from System.Win32.Process in
the Win32
package.
Since: process-1.6.12.0
createProcess :: CreateProcess -> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) #
This is the most general way to spawn an external process. The
process can be a command line to be executed by a shell or a raw command
with a list of arguments. The stdin, stdout, and stderr streams of
the new process may individually be attached to new pipes, to existing
Handle
s, or just inherited from the parent (the default.)
The details of how to create the process are passed in the
CreateProcess
record. To make it easier to construct a
CreateProcess
, the functions proc
and shell
are supplied that
fill in the fields with default values which can be overriden as
needed.
createProcess
returns (mb_stdin_hdl, mb_stdout_hdl, mb_stderr_hdl, ph)
,
where
- if
, thenstd_in
==CreatePipe
mb_stdin_hdl
will beJust h
, whereh
is the write end of the pipe connected to the child process'sstdin
. - otherwise,
mb_stdin_hdl == Nothing
Similarly for mb_stdout_hdl
and mb_stderr_hdl
.
For example, to execute a simple ls
command:
r <- createProcess (proc "ls" [])
To create a pipe from which to read the output of ls
:
(_, Just hout, _, _) <- createProcess (proc "ls" []){ std_out = CreatePipe }
To also set the directory in which to run ls
:
(_, Just hout, _, _) <- createProcess (proc "ls" []){ cwd = Just "/home/bob", std_out = CreatePipe }
Note that Handle
s provided for std_in
, std_out
, or std_err
via the
UseHandle
constructor will be closed by calling this function. This is not
always the desired behavior. In cases where you would like to leave the
Handle
open after spawning the child process, please use createProcess_
instead. All created Handle
s are initially in text mode; if you need them
to be in binary mode then use hSetBinaryMode
.
ph
contains a handle to the running process. On Windows
use_process_jobs
can be set in CreateProcess in order to create a
Win32 Job object to monitor a process tree's progress. If it is set
then that job is also returned inside ph
. ph
can be used to
kill all running sub-processes. This feature has been available since
1.5.0.0.
cleanupProcess :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO () #
Cleans up the process.
This function is meant to be invoked from any application level cleanup
handler. It terminates the process, and closes any CreatePipe
handle
s.
Since: process-1.6.4.0
callProcess :: FilePath -> [String] -> IO () #
Creates a new process to run the specified command with the given arguments, and wait for it to finish. If the command returns a non-zero exit code, an exception is raised.
If an asynchronous exception is thrown to the thread executing
callProcess
, the forked process will be terminated and
callProcess
will wait (block) until the process has been
terminated.
Since: process-1.2.0.0
callCommand :: String -> IO () #
Creates a new process to run the specified shell command. If the command returns a non-zero exit code, an exception is raised.
If an asynchronous exception is thrown to the thread executing
callCommand
, the forked process will be terminated and
callCommand
will wait (block) until the process has been
terminated.
Since: process-1.2.0.0
Arguments
:: ProcessHandle | A process in the process group |
-> IO () |
Sends an interrupt signal to the process group of the given process.
On Unix systems, it sends the group the SIGINT signal.
On Windows systems, it generates a CTRL_BREAK_EVENT and will only work for
processes created using createProcess
and setting the create_group
flag
Arguments
:: String | function name (for error messages) |
-> CreateProcess | |
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) |
This function is almost identical to
createProcess
. The only differences are:
Handle
s provided viaUseHandle
are not closed automatically.- This function takes an extra
String
argument to be used in creating error messages.
This function has been available from the System.Process.Internals module for some time, and is part of the System.Process module since version 1.2.1.0.
Since: process-1.2.1.0
createPipeFd :: IO (FD, FD) #
Create a pipe for interprocess communication and return a
(readEnd, writeEnd)
FD
pair.
Since: process-1.4.2.0
createPipe :: IO (Handle, Handle) #
Create a pipe for interprocess communication and return a
(readEnd, writeEnd)
Handle
pair.
Since: process-1.2.1.0
Constructors
Inherit | Inherit Handle from parent |
UseHandle Handle | Use the supplied Handle |
CreatePipe | Create a new pipe. The returned
|
NoStream | Close the stream's file descriptor without
passing a Handle. On POSIX systems this may
lead to strange behavior in the child process
because attempting to read or write after the
file has been closed throws an error. This
should only be used with child processes that
don't use the file descriptor at all. If you
wish to ignore the child process's output you
should either create a pipe and drain it
manually or pass a |
data ProcessHandle #
A handle to a process, which can be used to wait for termination
of the process using waitForProcess
.
None of the process-creation functions in this library wait for
termination: they all return a ProcessHandle
which may be used
to wait for the process later.
On Windows a second wait method can be used to block for event completion. This requires two handles. A process job handle and a events handle to monitor.
Instances
AsProcessHandle ProcessHandle Source # | |
Defined in Control.Process.ProcessHandle Methods _ProcessHandle :: Prism' ProcessHandle ProcessHandle Source # | |
HasProcessHandle ProcessHandle Source # | |
Defined in Control.Process.ProcessHandle Methods |
data CreateProcess #
Instances
Constructors
ShellCommand String | A command line to execute using the shell |
RawCommand FilePath [String] | The name of an executable with a list of arguments The
|
Instances
IsString CmdSpec | construct a Since: process-1.2.1.0 |
Defined in System.Process.Common Methods fromString :: String -> CmdSpec # | |
Show CmdSpec | |
AsCmdSpec CmdSpec Source # | |
Defined in Control.Process.CmdSpec | |
HasCmdSpec CmdSpec Source # | |
Defined in Control.Process.CmdSpec Methods cmdSpec :: Lens' CmdSpec CmdSpec Source # shellCommand :: Traversal' CmdSpec String Source # rawCommand :: Traversal' CmdSpec (FilePath, [String]) Source # rawCommandExe :: Traversal' CmdSpec FilePath Source # rawCommandArgumentList :: Traversal' CmdSpec [String] Source # | |
Eq CmdSpec | |
readCreateProcessWithExitCode :: Exception e' => CreateProcess -> String -> ExitcodeT (ExceptT e' IO) (String, String) (String, String) Source #
readProcessWithExitCode :: Exception e' => FilePath -> [String] -> String -> ExitcodeT (ExceptT e' IO) (String, String) (String, String) Source #
waitForProcess :: Exception e' => ProcessHandle -> ExitcodeT (ExceptT e' IO) () () Source #
getProcessExitCode :: Exception e' => ProcessHandle -> ExitcodeT (ExceptT e' IO) (Maybe ()) (Maybe ()) Source #
getProcessExitCodeBool :: Exception e' => ProcessHandle -> ExitcodeT (ExceptT e' IO) Bool Bool Source #