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

109
LINES

< > BotCompany Repo | #1001224 // Boot Bot

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (5306L/37K/121K).

!752

m {
  static new L<S> programsToStart;
  static new L<Boot> bootLog;
  static int sleepTime = 60;
  static new Flag stop;
  static new Flag booted;
  
  static boolean stayAliveAfterBoot = false;
  
  !include #1000915 // Flag class
  
  static class Boot {
    long when;
    S uptime;
  }
  
  p {
    if (args.length != 0 && args[0].equals("nosleep"))
      sleepTime = 0;
      
    readLocally("programsToStart");
    readLocally("bootLog");
    
    makeAndroid3("Boot Bot.");
    
    new Boot b;
    b.when = now();
    b.uptime = backtick(isWindows() ? "systeminfo" : "uptime");
    bootLog.add(b);
    saveLocally("bootLog");
    
    print("Booting at: " + b.when + ", uptime: " + b.uptime);
    print();
    
    print("PID: " + getPID() + ". BOOT BOT sleeping for 60 to be possibly killed before really doing something (starting these programs: " + structure(programsToStart) + ").");

    sleepSeconds(sleepTime); // Allow someone higher up :o)))) to "kill" me
    
    print("Bot Boot waking up.");
    
    if (stop.isUp())
      print("Boot cancelled.");
    else {
      int i = 1;
      for (S program : programsToStart) {
        print("Starting program " + i + "/" + programsToStart.size() + ": " + program);
        ++i;
        nohupJavax(program);
        sleepSeconds(5);
      }
      booted.raise();
      print("Done starting programs.");
    }
    
    if (stayAliveAfterBoot) {
      print("Sleeping as bot.");
      sleep();
    } else {
      print("Exiting.");
      System.exit(0);
    }
  }
  
  static synchronized S answer(S s, L<S> history) {
    new Matches m;
    if (match3("please add program * to boot list.", s, m)) {
      S p = unquote(m.m[0]);
      boolean isNew = !programsToStart.contains(p);
      if (isNew) {
        programsToStart.add(p);
        saveLocally("programsToStart");
        nohupJavax(p);
      }
      return isNew ? "OK, added and started." : "OK, was there already.";
    }
    
    if (match3("please remove program * from boot list.", s, m)) {
      S p = unquote(m.m[0]);
      programsToStart.remove(p);
      saveLocally("programsToStart");
      return "OK, removed.";
    }
    
    if (match3("stop", s) || match3("stop boot", s) || match3("stop booting", s)) {
      stop.raise();
      return "OK, boot cancelled.";
    }
    
    if (match3("have you booted", s))
      return booted.isUp() ? "yes" : (stop.isUp() ? "no, boot was stopped" : "not yet but soon");
      
    if (match3("reboot", s)) {
      final S cmd = isWindows() ? "shutdown -t 5 -r" : "reboot";
      print(cmd);
      S answer = "Rebooting in 5 seconds!";
      thread {
        sleepSeconds(5);
        backtick(cmd);
      }
      return answer;    
    }
    
    S answer = standardQuery(s, "programsToStart");
    if (answer == null) answer = additionalQuery(s, "bootLog", "boot");
    return answer;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, dhtvkmknsjym, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
1115 stefan See also: http://stackoverflow.com/questions/11809191/linux-launch-java-program-on-startup-ec2-instance 2015-10-05 18:18:16

add comment

Snippet ID: #1001224
Snippet name: Boot Bot
Eternal ID of this version: #1001224/1
Text MD5: 45f75e8ad7f735b03e7800c4fa83a812
Transpilation MD5: f1616788c190fe61671e5216415bf67f
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-30 20:55:49
Source code size: 2980 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 969 / 1578
Referenced in: #1001465 - Program refs
#3000050 - Contents of http://tinybrain.blog.de
#3000195 - Answer for stefanreich (>> y)
#3000196 - Answer for stefanreich (>> y)
#3000197 - Answer for stefanreich (>> program data sizes)
#3000198 - Answer for stefanreich (>> program data sizes)
#3000199 - Answer for stefanreich (>> program data sizes)
#3000200 - Answer for stefanreich (>> program data sizes)
#3000201 - Answer for stefanreich (>> list all files)
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)