Class Exec

java.lang.Object
com.peterphi.std.system.exec.Exec

public class Exec extends Object
An abstraction over ProcessBuilder to simplify the creation of processes
  • Field Details

  • Constructor Details

  • Method Details

    • customiseProcessBuilder

      public Exec customiseProcessBuilder(Consumer<ProcessBuilder> customiser)
      Apply customisations to the internal ProcessBuilder
      Parameters:
      customiser - custom logic to run
      Returns:
      this
    • getWorkingDirectory

      public File getWorkingDirectory()
    • setWorkingDirectory

      public Exec setWorkingDirectory(File dir)
    • setEnv

      public Exec setEnv(String key, String value)
    • getEnv

      public String getEnv(String key)
    • runAsSuperuser

      public Exec runAsSuperuser()
    • runAs

      public Exec runAs(String username)
    • getRedirectError

      public boolean getRedirectError()
    • setRedirectError

      public Exec setRedirectError(boolean value)
    • setRedirectError

      public Exec setRedirectError(File file)
    • setRedirectOutput

      public Exec setRedirectOutput(File file)
    • startBasic

      public BasicProcessTracker startBasic() throws IOException
      Launches the process, returning a handle to it for IO ops, etc
      the caller must read the output streams: otherwise the buffers may fill up and the remote program will be suspended indefinitely
      Returns:
      Throws:
      IOException
    • start

      public Execed start() throws IOException
      Launches the process, returning a handle to it for IO ops, etc.
      The finish condition for the OutputProcess is that all processes outputting to standard out must be complete before proceeding
      Returns:
      Throws:
      IOException
    • getProcessBuilder

      public ProcessBuilder getProcessBuilder()
      Returns a ProcessBuilder for use in a manual launching
      Returns:
    • join

      protected static String join(String with, String... strings)
    • join

      protected static String join(String with, List<String> strings)
    • rootUtility

      public static Execed rootUtility(String... command) throws IOException
      Runs a command in "utility" mode: redirecting stderr to stdout and running as root
      Parameters:
      command -
      Returns:
      Throws:
      IOException
    • rootUtility

      public static Execed rootUtility(Iterable<String> command) throws IOException
      Runs a command in "utility" mode: redirecting stderr to stdout and running as root
      Parameters:
      command -
      Returns:
      Throws:
      IOException
    • utility

      public static Execed utility(String... command) throws IOException
      Runs a command in "utility" mode: redirecting stderr to stdout and optionally executing as a different user (eg root)
      Parameters:
      command -
      Returns:
      Throws:
      IOException
    • utility

      public static Execed utility(Iterable<String> command) throws IOException
      Runs a command in "utility" mode: redirecting stderr to stdout and optionally executing as a different user (eg root)
      Parameters:
      command -
      Returns:
      Throws:
      IOException
    • utilityAs

      public static Execed utilityAs(String as, String... command) throws IOException
      Runs a command in "utility" mode: redirecting stderr to stdout and optionally executing as a different user (eg root)
      Parameters:
      as -
      command -
      Returns:
      Throws:
      IOException
    • utilityAs

      public static Execed utilityAs(String as, Iterable<String> command) throws IOException
      Runs a command in "utility" mode: redirecting stderr to stdout and optionally executing as a different user (eg root)
      Parameters:
      as -
      command -
      Returns:
      Throws:
      IOException
    • appAs

      public static Execed appAs(String as, String... command) throws IOException
      Runs a command, optionally executing as a different user (eg root)
      Parameters:
      as -
      command -
      Returns:
      Throws:
      IOException
    • appAs

      public static Execed appAs(String as, Iterable<String> command) throws IOException
      Runs a command, optionally executing as a different user (eg root)
      Parameters:
      as -
      command -
      Returns:
      Throws:
      IOException