Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created May 24, 2012 11:16
Show Gist options
  • Save FeepingCreature/2780868 to your computer and use it in GitHub Desktop.
Save FeepingCreature/2780868 to your computer and use it in GitHub Desktop.
module restarter;
import std.string, std.file, std.thread, std.time, std.fun;
extern(C) {
alias pid_t = int;
pid_t fork();
int execv(char* path, char** argv);
pid_t waitpid(pid_t, int* status, int options);
}
void main() {
auto serverlist = string: readAll "serverlist.txt"
#.split("\n")
#.select(\(string s) -> !s.startsWith("#"))
#.map \(string s) -> s.split(" ")
#.eval[];
auto tp = new ThreadPool serverlist.length;
for auto entry <- serverlist {
tp.addTask new \{
while (true) {
auto pid = fork();
if (!pid) {
char*[auto~] argv;
argv ~= char*:"idc";
for auto arg <- entry argv ~= toStringz arg;
argv ~= null;
execv("idc", argv.ptr);
}
waitpid(pid, null, 0);
writeln "idc $entry terminated. Restarting in 5 .. ";
for (int i = 4; i > 0; --i) { sleep 1; writeln "$i .. "; }
}
}
}
tp.waitComplete;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment