Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

57
LINES

< > BotCompany Repo | #1000828 - nohup function

JavaX fragment (include)

sbool nohup_debug;
sbool nohup_noSlashB;
static new ThreadLocal<Int> nohup_exitValue;

public static File nohup(String cmd) ctex {
  File outFile = File.createTempFile("nohup_" + nohup_sanitize(cmd) + "_", ".out");
  nohup(cmd, outFile, false);
  return outFile;
}

/** outFile takes stdout and stderr. */
public static void nohup(String cmd, File outFile, boolean append) ctex {
  String command = nohup_makeNohupCommand(cmd, outFile, append);

  File scriptFile = File.createTempFile("_realnohup", isWindows() ? ".bat" : "");
  System.out.println("[Nohup] " + command);
  try {
    if (nohup_debug)
      print("[nohup] Script file: " + scriptFile.getPath());
    saveTextFile(scriptFile.getPath(), command);
    String[] command2;
    if (isWindows())
      if (nohup_noSlashB)
        command2 = new String[] {"cmd", "/c", "start", scriptFile.getPath() };
      else
        command2 = new String[] {"cmd", "/c", "start", "/b", scriptFile.getPath() };
    else
      command2 = new String[] {"/bin/bash", scriptFile.getPath() };
      
    printStruct("[nohup] " , command2);
    Process process = Runtime.getRuntime().exec(command2);
    try {
      process.waitFor();
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
    int value = process.exitValue();
    nohup_exitValue.set(value);
    if (value != 0)
      warn("nohup exit value != 0: " + value);
    //System.out.println("exit value: " + value);
  } finally {
    if (!isWindows())
      scriptFile.delete();
  }
}

public static String nohup_makeNohupCommand(String cmd, File outFile, boolean append) {
  mkdirsForFile(outFile);

  String command;
  if (isWindows())
    command = cmd + (append ? " >>" : " >") + winQuote(outFile.getPath()) + " 2>&1";
  else
    command = "nohup " + cmd + (append ? " >>" : " >") + bashQuote(outFile.getPath()) + " 2>&1 &";
  return command;
}

download  show line numbers  debug dex   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lnbujpyubztb, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000828
Snippet name: nohup function
Eternal ID of this version: #1000828/10
Text MD5: 45bec2867a29721830f3e97370eb88b6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-31 19:04:16
Source code size: 1932 bytes / 57 lines
Pitched / IR pitched: No / No
Views / Downloads: 443 / 2478
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1000828 & http://1000828.tinybrain.de