code: def run_command(
      runtime, output=None, timeout=0.01, write_cmdline=False
  ):
      """Run a command, read stdout and stderr, prefix with timestamp.
  
      The returned runtime contains a merged stdout+stderr log with timestamps
      """
  
      # Init variables
      cmdline = runtime.cmdline
      env = canonicalize_env(runtime.environ)
  
      errfile = None
      outfile = None
      stdout = PIPE
      stderr = PIPE
  
      if output == "file":
          outfile = os.path.join(runtime.cwd, "output.nipype")
          stdout = open(outfile, "wb")  # t=='text'===default
          stderr = STDOUT
      elif output == "file_split":
          outfile = os.path.join(runtime.cwd, "stdout.nipype")
          stdout = open(outfile, "wb")
          errfile = os.path.join(runtime.cwd, "stderr.nipype")
          stderr = open(errfile, "wb")
      elif output == "file_stdout":
          outfile = os.path.join(runtime.cwd, "stdout.nipype")
          stdout = open(outfile, "wb")
      elif output == "file_stderr":
          errfile = os.path.join(runtime.cwd, "stderr.nipype")
          stderr = open(errfile, "wb")
  
      if write_cmdline:
          (Path(runtime.cwd) / "command.txt").write_text(
              cmdline, encoding="utf-8"
          )
  
      proc = Popen(
          cmdline,
          stdout=stdout,
          stderr=stderr,
          shell=True,
          cwd=runtime.cwd,
          env=env,
          close_fds=(not sys.platform.startswith("win")),
      )
  
      result = {
          "stdout": [],
          "stderr": [],
          "merged": [],
      }
  
      if output == "stream":
          streams = [
              Stream("stdout", proc.stdout),
              Stream("stderr", proc.stderr),
          ]
  
          def _process(drain=0):
              try:
                  res = select.select(streams, [], [], timeout)
              except OSError as e:
                  iflogger.info(e)
                  if e.errno == errno.EINTR:
                      return
                  else:
                      raise
              else:
                  for stream in res[0]:
                      stream.read(drain)
  
          while proc.returncode is None:
              proc.poll()
              _process()
  
          _process(drain=1)
  
          # collect results, merge and return
          result = {}
          temp = []
          for stream in streams:
              rows = stream._rows
              temp += rows
              result[stream._name] = [r[2] for r in rows]
          temp.sort()
          result["merged"] = [r[1] for r in temp]
  
      if output.startswith("file"):
          proc.wait()
          if outfile is not None:
              stdout.flush()
              stdout.close()
              with open(outfile, "rb") as ofh:
                  stdoutstr = ofh.read()
              result["stdout"] = read_stream(stdoutstr, logger=iflogger)
              del stdoutstr
  
          if errfile is not None:
              stderr.flush()
              stderr.close()
              with open(errfile, "rb") as efh:
                  stderrstr = efh.read()
              result["stderr"] = read_stream(stderrstr, logger=iflogger)
              del stderrstr
  
          if output == "file":
              result["merged"] = result["stdout"]
              result["stdout"] = []
      else:
          stdout, stderr = proc.communicate()
          if (
              output == "allatonce"
          ):  # Discard stdout and stderr otherwise
              result["stdout"] = read_stream(stdout, logger=iflogger)
              result["stderr"] = read_stream(stderr, logger=iflogger)
  
      runtime.returncode = proc.returncode
      try:
          proc.terminate()  # Ensure we are done
      except OSError as error:
          # Python 2 raises when the process is already gone
          if error.errno != errno.ESRCH:
              raise
  
      # Dereference & force GC for a cleanup
      del proc
      del stdout
      del stderr
      gc.collect()
  
      runtime.stderr = "\n".join(result["stderr"])
      runtime.stdout = "\n".join(result["stdout"])
      runtime.merged = "\n".join(result["merged"])
      return runtime
  
file_path: nipype/nipype/utils/subprocess.py
imports: import os
  import sys
  import gc
  import errno
  import select
  import locale
  import datetime
  from pathlib import Path
  from subprocess import Popen, STDOUT, PIPE
  from .filemanip import canonicalize_env, read_stream
  from ..None import logging
label_desc: Use image node_id 1 for calling a global function with example usage: run_command(runtime, output, timeout, write_cmdline) and returns: runtime