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.

1  
sbool nohup_debug;
2  
sbool nohup_noSlashB;
3  
sbool nohup_keepScriptFile;
4  
static new ThreadLocal<Int> nohup_exitValue;
5  
6  
public static File nohup(File cmd) {
7  
  ret nohup(f2s(cmd));
8  
}
9  
10  
public static File nohup(String cmd) ctex {
11  
  File outFile = File.createTempFile("nohup_" + nohup_sanitize(cmd) + "_", ".out");
12  
  nohup(cmd, outFile, false);
13  
  return outFile;
14  
}
15  
16  
/** outFile takes stdout and stderr. */
17  
public static void nohup(String cmd, File outFile, boolean append) ctex {
18  
  S command = nohup_makeNohupCommand(cmd, outFile, append);
19  
20  
  File scriptFile = File.createTempFile("_realnohup", isWindows() ? ".bat" : "");
21  
  print("[nohup] " + command);
22  
  try {
23  
    if (nohup_debug)
24  
      print("[nohup] Script file: " + scriptFile.getPath());
25  
    saveTextFile(scriptFile.getPath(), command);
26  
    S[] command2;
27  
    if (isWindows())
28  
      if (nohup_noSlashB)
29  
        command2 = new String[] {"cmd", "/c", "start", scriptFile.getPath() };
30  
      else
31  
        command2 = new String[] {"cmd", "/c", "start", "/b", scriptFile.getPath() };
32  
    else
33  
      command2 = new String[] {"/bin/bash", scriptFile.getPath() };
34  
      
35  
    print("[nohup] " + joinWithSpace(quoteAll(command2)));
36  
    Process process = Runtime.getRuntime().exec(command2);
37  
    try {
38  
      process.waitFor();
39  
    } catch (InterruptedException e) {
40  
      throw new RuntimeException(e);
41  
    }
42  
    int value = process.exitValue();
43  
    nohup_exitValue.set(value);
44  
    if (value != 0)
45  
      warn("nohup exit value != 0: " + value);
46  
    //System.out.println("exit value: " + value);
47  
  } finally {
48  
    if (!nohup_keepScriptFile && !isWindows())
49  
      scriptFile.delete();
50  
  }
51  
}
52  
53  
sS nohup_makeNohupCommand(String cmd, File outFile, boolean append) {
54  
  mkdirsForFile(outFile);
55  
56  
  String command;
57  
  if (isWindows())
58  
    command = cmd + (append ? " >>" : " >") + winQuote(outFile.getPath()) + " 2>&1";
59  
  else
60  
    command = "nohup " + cmd + (append ? " >>" : " >") + bashQuote(outFile.getPath()) + " 2>&1 &";
61  
  ret command;
62  
}

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: 683 / 2836
Version history: 12 change(s)
Referenced in: [show references]