Original code copyright (C) 2009-2022 Rudolf Cardinal (

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Support functions for process/external command management.

cardinal_pythonlib.process.get_external_command_output(command: str) bytes[source]

Takes a command-line command, executes it, and returns its stdout output.


command – command string


output from the command as bytes

cardinal_pythonlib.process.get_pipe_series_output(commands: Sequence[str], stdinput: BinaryIO | None = None) bytes[source]

Get the output from a piped series of commands.

  • commands – sequence of command strings

  • stdinput – optional stdin data to feed into the start of the pipe


stdout from the end of the pipe

cardinal_pythonlib.process.kill_proc_tree(pid: int, including_parent: bool = True, timeout_s: float = 5) Tuple[Set[Process], Set[Process]][source]

Kills a tree of processes, starting with the parent. Slightly modified from

  • pid – process ID of the parent

  • including_parent – kill the parent too?

  • timeout_s – timeout to wait for processes to close


(gone, still_alive), where both are sets of psutil.Process objects

Return type:


cardinal_pythonlib.process.launch_external_file(filename: str, raise_if_fails: bool = False) None[source]

Launches a file using the operating system’s standard launcher.

  • filename – file to launch

  • raise_if_fails – raise any exceptions from["xdg-open", filename]) (Linux) or os.startfile(filename) (otherwise)? If not, exceptions are suppressed.

cardinal_pythonlib.process.nice_call(*popenargs, timeout: float | None = None, cleanup_timeout: float | None = None, **kwargs) int[source]

Like, but give the child process time to clean up and communicate if a KeyboardInterrupt is raised.

Modified from