Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Created October 5, 2017 08:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekroTJA/2b2e10f17f5e2af3a3799efcab309061 to your computer and use it in GitHub Desktop.
Save zekroTJA/2b2e10f17f5e2af3a3799efcab309061 to your computer and use it in GitHub Desktop.
A little script for managing multiple discord bots on an linux server
# DISCORD BOT MANAGER - © 2017 Ringo Hoffmann (zekro Development)
# READ BEFORE USAGE: http://s.zekro.de/codepolicy
# To use this tool, you need to create a path structure like following:
# ..
# ├── bot1
# │ ├── ...bot stuff...
# │ └── run.sh
# │
# ├── bot2
# │ ├── ...bot stuff...
# │ └── run.sh
# │
# ├── start.py
# └── start
# The files 'run.sh' in each bot's root directory containing only the start command of the bot, for example:
# java -jar DiscordBot.jar
# or for a python bot:
# python3.6 bot.py
# or for Node:
# node bot.js
# and so on...
# The 'start.py' file is this file here. You can name it how you want.
# The file 'start' is optional and contains just the run command for this script:
# python3 start.py
# So it's easier entering 'sh start' in the console than 'python3 start.py'.
import sys
import os
import subprocess
import json
import psutil
args = sys.argv
dirs = os.listdir(".")
servers = []
inp = ""
VERSION = "1.2"
class Color:
W = '\033[0m' # white (normal)
R = '\033[31m' # red
G = '\033[32m' # green
O = '\033[33m' # orange
B = '\033[34m' # blue
P = '\033[35m' # purple
def error(content):
print(Color.R + "[ERROR] "+ content + Color.W)
input()
def isRunning(name):
proc = subprocess.Popen(["screen", "-ls"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return name in str(proc.stdout.read())
def isAnyRunning():
for s in servers:
if isRunning(s):
return True
return False
def clear():
subprocess.call("clear")
def getSys():
cpulaod = psutil.cpu_percent()
mem = psutil.virtual_memory()
memtotal = mem.total / 1024 / 1024
memused = mem.used / 1024 / 1024
memload = mem.percent
disk = psutil.disk_usage(".")
disktotal = disk.total / 1024 / 1024 / 1024
diskused = disk.used / 1024 / 1024 / 1024
diskload = disk.percent
return str("CPU load: %d %%\n"
"Memory (used): %d MiB / %d MiB (%d %%)\n"
"Space (used): %d GiB / %d GiB (%d %%)"
% (cpulaod, memused, memtotal, memload, diskused, disktotal, diskload))
def checkForRun(server):
if not os.path.isfile(server + "/run.sh"):
error("'run.sh' does not exist in server location '%s'!" % server)
input()
return True
return False
def printMenu():
global servers
clear()
print(Color.B + "Server Management System v." + VERSION + " - (c) 2017 zekro\n" + Color.W)
print("SYSTEM STATS: \n" + getSys() + "\n")
servers = [d for d in dirs if os.path.isdir(d)]
for i, d in enumerate(servers):
running = Color.G + "[RUNNING]" + Color.W if isRunning(d) else Color.O + "[STOPPED]" + Color.W
print("%s [%d] %s" % (running, i + 1, d))
return input("\nPlease enter a command\nType 'help' for command list\n\n> ")
def runMain():
global inp
inp = printMenu()
# MAIN PROGRAM
runMain()
while not "exit" in inp:
invoke = inp.split()[0].lower()
args = inp.split()[1:]
if len(args) > 0:
if invoke == "start":
index = int(args[0]) - 1
if index >= len(servers) or index < 0:
error("Please enter a valid number!")
else:
server = servers[index]
if isRunning(server):
error("This server is currently still running!")
elif checkForRun(server):
break
else:
os.chdir(server)
subprocess.call(["screen", "-S", server, "-L", "sh", "run.sh"])
exit()
elif invoke == "stop":
index = int(args[0]) - 1
if index >= len(servers) or index < 0:
error("Please enter a valid number!")
else:
server = servers[index]
if not isRunning(server):
error("This sever is currently running!")
else:
subprocess.call(["screen", "-X", "-S", server, "quit"])
elif invoke == "resume":
index = int(args[0]) - 1
if index >= len(servers) or index < 0:
error("Please enter a valid number!")
else:
server = servers[index]
if not isRunning(server):
error("This server is currently not running!")
else:
subprocess.call(["screen", "-r", server])
exit()
elif invoke == "restart":
index = int(args[0]) - 1
if index >= len(servers) or index < 0:
error("Please enter a valid number!")
else:
server = servers[index]
if not isRunning(server):
error("This server is currently not running!")
elif checkForRun(server):
break
else:
print("Stopping server...")
subprocess.call(["screen", "-X", "-S", server, "quit"])
print("Press enter to restart server...")
input()
print("Starting server...")
subprocess.call(["screen", "-S", server, "-L", "sh", "run.sh"])
elif invoke == "help" or invoke == "":
print(
"\n"
" start <index> - Start a stopped server\n"
" stop <index> - Stop a running server\n"
" resume <index> - Resume a running screen\n"
" restart <index> - Restart a server\n"
" exit - Exit this tool\n"
)
print("[Press any key to continue...]")
input()
else:
error("Please enter a valid command or type 'help' for all commands!")
runMain()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment