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

62
LINES

< > BotCompany Repo | #1000828 // nohup function

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (5620L) is out of date.

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

public static File nohup(File cmd) {
  ret nohup(f2s(cmd));
}

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 {
  S command = nohup_makeNohupCommand(cmd, outFile, append);

  File scriptFile = File.createTempFile("_realnohup", isWindows() ? ".bat" : "");
  print("[nohup] " + command);
  try {
    if (nohup_debug)
      print("[nohup] Script file: " + scriptFile.getPath());
    saveTextFile(scriptFile.getPath(), command);
    S[] 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() };
      
    print("[nohup] " + joinWithSpace(quoteAll(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 (!nohup_keepScriptFile && !isWindows())
      scriptFile.delete();
  }
}

sS 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 &";
  ret command;
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000828
Snippet name: nohup function
Eternal ID of this version: #1000828/13
Text MD5: 754b0c9e208c0b1a8a3b7b854e3de60a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-28 22:50:11
Source code size: 2028 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 660 / 2806
Version history: 12 change(s)
Referenced in: [show references]