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

57
LINES

< > BotCompany Repo | #1000828 - nohup function

JavaX fragment (include)

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

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: 447 / 2483
Version history: 9 change(s)
Referenced in: [show references]

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