import java.io.IOException; import java.nio.file.Path; /** * Realization of class dedicated to the control * of GNU Screen sessions *

* Created by daniil on 02.04.16. */ public class ScreenLib { /** * Start a screen with certain ID * and execute a command inside it * * @param screenName Screen ID * @param runPath Home path of started screen * @param command Command [+args] to execute inside the screen * @throws IOException */ public void startScreen(String screenName, Path runPath, String... command) throws IOException { assert screenName != null && screenName.length() != 0; assert runPath != null; // Merge two arrays String[] commandBase = new String[command.length + 3]; commandBase[0] = "screen"; commandBase[1] = "-dmS"; commandBase[2] = screenName; System.arraycopy(command, 0, commandBase, 3, command.length); ProcessBuilder processBuilder = new ProcessBuilder(commandBase); processBuilder.directory(runPath.toFile()); processBuilder.start(); } /** * Send command to specific screen * * @param screenName Screen ID * @param command Command to send * @throws IOException */ public void sendCommand(String screenName, String command) throws IOException { assert screenName != null; assert command != null; ProcessBuilder processBuilder = new ProcessBuilder("screen", "-S", screenName, "-p", "0", "-X", "stuff", String.format(command + "%n")); processBuilder.start(); } /** * Force terminate a screen session * * @param screenName Screen ID * @throws IOException */ public void killScreen(String screenName) throws IOException { assert screenName != null; ProcessBuilder processBuilder = new ProcessBuilder("screen", "-X", "-S", screenName, "quit"); processBuilder.start(); } }