##
6.1.5 Process Management

These functions may be used to create and manage processes.

The various `exec*()` functions take a list of arguments for the new program loaded into the process. In each case, the first of these arguments is passed to the new program as its own name rather than as an argument a user may have typed on a command line. For the C programmer, this is the `argv[0]` passed to a program's `main()`. For example, "`os.execv('/bin/echo', ['foo', 'bar'])`" will only print "`bar`" on standard output; "`foo`"will seem to be ignored.

`abort`( | )

  Generate a `SIGABRT` signal to the current process. On Unix, the default behavior is to produce a core dump; on Windows, the process immediately returns an exit code of `3`. Be aware that programs which use `signal.signal()` to register a handler for `SIGABRT` will behave differently. Availability: Unix, Windows.

`execl`( | path, arg0, arg1, ...)

`execle`( | path, arg0, arg1, ..., env)

`execlp`( | file, arg0, arg1, ...)

`execlpe`( | file, arg0, arg1, ..., env)

`execv`( | path, args)

`execve`( | path, args, env)

`execvp`( | file, args)

`execvpe`( | file, args, env)

  These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process ID as the caller. Errors will be reported as `OSError` exceptions.

  The "`l`" and "`v`" variants of the `exec*()` functions differ in how command-line arguments are passed. The "`l`" variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the `execl*()` functions. The "`v`" variants are good when the number of parameters is variable, with the arguments being passed in a list or tuple as the args parameter. In either case, the arguments to the child process must start with the name of the command being run.

  The variants which include a "`p`" near the end (`execlp()`, `execlpe()`, `execvp()`, and `execvpe()`) will use the PATH environment variable to locate the program file. When the environment is being replaced (using one of the `exec*e()` variants, discussed in the next paragraph), the new environment is used as the source of the PATH variable. The other variants, `execl()`, `execle()`, `execv()`, and `execve()`, will not use the PATH variable to locate the executable; path must contain an appropriate absolute or relative path.

  For `execle()`, `execlpe()`, `execve()`, and `execvpe()` (note that these all end in "`e`"), the env parameter must be a mapping which is used to define the environment variables for the new process; the `execl()`, `execlp()`, `execv()`, and `execvp()` all cause the new process to inherit the environment of the current process. Availability: Unix, Windows.

`_exit`( | n)

  Exit to the system with status n, without calling cleanup handlers, flushing stdio buffers, etc. Availability: Unix, Windows.

  Note: the standard way to exit is `sys.exit(n)`. `_exit()` should normally only be used in the child process after a `fork()`.

The following exit codes are a defined, and can be used with `_exit()`, although they are not required. These are typically used for system programs written in Python, such as a mail server's external command delivery program.

`EX_OK`
  Exit code that means no error occurred. Availability: Unix. New in version 2.3.

`EX_USAGE`
  Exit code that means the command was used incorrectly, such as when the wrong number of arguments are given. Availability: Unix. New in version 2.3.

`EX_DATAERR`
  Exit code that means the input data was incorrect. Availability: Unix. New in version 2.3.

`EX_NOINPUT`
  Exit code that means an input file did not exist or was not readable. Availability: Unix. New in version 2.3.

`EX_NOUSER`
  Exit code that means a specified user did not exist. Availability: Unix. New in version 2.3.

`EX_NOHOST`
  Exit code that means a specified host did not exist. Availability: Unix. New in version 2.3.

`EX_UNAVAILABLE`
  Exit code that means that a required service is unavailable. Availability: Unix. New in version 2.3.

`EX_SOFTWARE`
  Exit code that means an internal software error was detected. Availability: Unix. New in version 2.3.

`EX_OSERR`
  Exit code that means an operating system error was detected, such as the inability to fork or create a pipe. Availability: Unix. New in version 2.3.

`EX_OSFILE`
  Exit code that means some system file did not exist, could not be opened, or had some other kind of error. Availability: Unix. New in version 2.3.

`EX_CANTCREAT`
  Exit code that means a user specified output file could not be created. Availability: Unix. New in version 2.3.

`EX_IOERR`
  Exit code that means that an error occurred while doing I/O on some file. Availability: Unix. New in version 2.3.

`EX_TEMPFAIL`
  Exit code that means a temporary failure occurred. This indicates something that may not really be an error, such as a network connection that couldn't be made during a retryable operation. Availability: Unix. New in version 2.3.

`EX_PROTOCOL`
  Exit code that means that a protocol exchange was illegal, invalid, or not understood. Availability: Unix. New in version 2.3.

`EX_NOPERM`
  Exit code that means that there were insufficient permissions to perform the operation (but not intended for file system problems). Availability: Unix. New in version 2.3.

`EX_CONFIG`
  Exit code that means that some kind of configuration error occurred. Availability: Unix. New in version 2.3.

`EX_NOTFOUND`
  Exit code that means something like ``an entry was not found''. Availability: Unix. New in version 2.3.

`fork`( | )

  Fork a child process. Return `0` in the child, the child's process id in the parent. Availability: Unix.

`forkpty`( | )

  Fork a child process, using a new pseudo-terminal as the child's controlling terminal. Return a pair of `(pid, fd)`, where pid is `0` in the child, the new child's process id in the parent, and fd is the file descriptor of the master end of the pseudo-terminal. For a more portable approach, use the `pty` module. Availability: Some flavors of Unix.

`kill`( | pid, sig)

  Kill the process pid with signal sig. Constants for the specific signals available on the host platform are defined in the `signal` module. Availability: Unix.

`killpg`( | pgid, sig)

  Kill the process group pgid with the signal sig. Availability: Unix. New in version 2.3.

`nice`( | increment)

  Add increment to the process's ``niceness''. Return the new niceness. Availability: Unix.

`plock`( | op)

  Lock program segments into memory. The value of op (defined in `<sys/lock.h>`) determines which segments are locked. Availability: Unix.

`popen`( | ...)

`popen2`( | ...)

`popen3`( | ...)

`popen4`( | ...)

  Run child processes, returning opened pipes for communications. These functions are described in section 6.1.2.

`spawnl`( | mode, path, ...)

`spawnle`( | mode, path, ..., env)

`spawnlp`( | mode, file, ...)

`spawnlpe`( | mode, file, ..., env)

`spawnv`( | mode, path, args)

`spawnve`( | mode, path, args, env)

`spawnvp`( | mode, file, args)

`spawnvpe`( | mode, file, args, env)

  Execute the program path in a new process. If mode is `P_NOWAIT`, this function returns the process ID of the new process; if mode is `P_WAIT`, returns the process's exit code if it exits normally, or `-signal`, where signal is the signal that killed the process. On Windows, the process ID will actually be the process handle, so can be used with the `waitpid()` function.

  The "`l`" and "`v`" variants of the `spawn*()` functions differ in how command-line arguments are passed. The "`l`" variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the `spawnl*()` functions. The "`v`" variants are good when the number of parameters is variable, with the arguments being passed in a list or tuple as the args parameter. In either case, the arguments to the child process must start with the name of the command being run.

  The variants which include a second "`p`" near the end (`spawnlp()`, `spawnlpe()`, `spawnvp()`, and `spawnvpe()`) will use the PATH environment variable to locate the program file. When the environment is being replaced (using one of the `spawn*e()` variants, discussed in the next paragraph), the new environment is used as the source of the PATH variable. The other variants, `spawnl()`, `spawnle()`, `spawnv()`, and `spawnve()`, will not use the PATH variable to locate the executable; path must contain an appropriate absolute or relative path.

  For `spawnle()`, `spawnlpe()`, `spawnve()`, and `spawnvpe()` (note that these all end in "`e`"), the env parameter must be a mapping which is used to define the environment variables for the new process; the `spawnl()`, `spawnlp()`, `spawnv()`, and `spawnvp()` all cause the new process to inherit the environment of the current process.

  As an example, the following calls to `spawnlp()` and `spawnvpe()` are equivalent:

      import os
      os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')

      L = ['cp', 'index.html', '/dev/null']
      os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)

  Availability: Unix, Windows. `spawnlp()`, `spawnlpe()`, `spawnvp()` and `spawnvpe()` are not available on Windows. New in version 1.6.

`P_NOWAIT`
`P_NOWAITO`
  Possible values for the mode parameter to the `spawn*()` family of functions. If either of these values is given, the `spawn*()` functions will return as soon as the new process has been created, with the process ID as the return value. Availability: Unix, Windows. New in version 1.6.

`P_WAIT`
  Possible value for the mode parameter to the `spawn*()` family of functions. If this is given as mode, the `spawn*()` functions will not return until the new process has run to completion and will return the exit code of the process the run is successful, or `-signal` if a signal kills the process. Availability: Unix, Windows. New in version 1.6.

`P_DETACH`
`P_OVERLAY`
  Possible values for the mode parameter to the `spawn*()` family of functions. These are less portable than those listed above. `P_DETACH` is similar to `P_NOWAIT`, but the new process is detached from the console of the calling process. If `P_OVERLAY` is used, the current process will be replaced; the `spawn*()` function will not return. Availability: Windows. New in version 1.6.

`startfile`( | path)

  Start a file with its associated application. This acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated.

  `startfile()` returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application's exit status. The path parameter is relative to the current directory. If you want to use an absolute path, make sure the first character is not a slash ("`/`"); the underlying Win32 `ShellExecute()` function doesn't work if it is. Use the `os.path.normpath()` function to ensure that the path is properly encoded for Win32. Availability: Windows. New in version 2.0.

`system`( | command)

  Execute the command (a string) in a subshell. This is implemented by calling the Standard C function `system()`, and has the same limitations. Changes to `posix.environ`, `sys.stdin`, etc. are not reflected in the environment of the executed command.

  On Unix, the return value is the exit status of the process encoded in the format specified for `wait()`. Note that POSIX does not specify the meaning of the return value of the C `system()` function, so the return value of the Python function is system-dependent.

  On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always `0`; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

  Availability: Unix, Windows.

`times`( | )

  Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. The items are: user time, system time, children's user time, children's system time, and elapsed real time since a fixed point in the past, in that order. See the Unix manual page times(2) or the corresponding Windows Platform API documentation. Availability: Unix, Windows.

`wait`( | )

  Wait for completion of a child process, and return a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced. Availability: Unix.

`waitpid`( | pid, options)

  The details of this function differ on Unix and Windows.

  On Unix: Wait for completion of a child process given by process id pid, and return a tuple containing its process id and exit status indication (encoded as for `wait()`). The semantics of the call are affected by the value of the integer options, which should be `0` for normal operation.

  If pid is greater than `0`, `waitpid()` requests status information for that specific process. If pid is `0`, the request is for the status of any child in the process group of the current process. If pid is `-1`, the request pertains to any child of the current process. If pid is less than `-1`, status is requested for any process in the process group `-pid` (the absolute value of pid).

  On Windows: Wait for completion of a process given by process handle pid, and return a tuple containing pid, and its exit status shifted left by 8 bits (shifting makes cross-platform use of the function easier). A pid less than or equal to `0` has no special meaning on Windows, and raises an exception. The value of integer options has no effect. pid can refer to any process whose id is known, not necessarily a child process. The `spawn()` functions called with `P_NOWAIT` return suitable process handles.

`WNOHANG`
  The option for `waitpid()` to avoid hanging if no child process status is available immediately. Availability: Unix.

`WCONTINUED`
  This option causes child processes to be reported if they have been continued from a job control stop since their status was last reported. Availability: Some Unix systems. New in version 2.3.

`WUNTRACED`
  This option causes child processes to be reported if they have been stopped but their current state has not been reported since they were stopped. Availability: Unix. New in version 2.3.

The following functions take a process status code as returned by `system()`, `wait()`, or `waitpid()` as a parameter. They may be used to determine the disposition of a process.

`WCOREDUMP`( | status)

  Returns `True` if a core dump was generated for the process, otherwise it returns `False`. Availability: Unix. New in version 2.3.

`WIFCONTINUED`( | status)

  Returns `True` if the process has been continued from a job control stop, otherwise it returns `False`. Availability: Unix. New in version 2.3.

`WIFSTOPPED`( | status)

  Returns `True` if the process has been stopped, otherwise it returns `False`. Availability: Unix.

`WIFSIGNALED`( | status)

  Returns `True` if the process exited due to a signal, otherwise it returns `False`. Availability: Unix.

`WIFEXITED`( | status)

  Returns `True` if the process exited using the exit(2) system call, otherwise it returns `False`. Availability: Unix.

`WEXITSTATUS`( | status)

  If `WIFEXITED(status)` is true, return the integer parameter to the exit(2) system call. Otherwise, the return value is meaningless. Availability: Unix.

`WSTOPSIG`( | status)

  Return the signal which caused the process to stop. Availability: Unix.

`WTERMSIG`( | status)

  Return the signal which caused the process to exit. Availability: Unix.