xmrig-docker/xmrig-ctl.py

62 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import subprocess
import sys
import time
fifo_file = "/tmp/xmrig.fifo"
def main():
if len(sys.argv) == 1:
return start()
elif len(sys.argv) == 2 and len(sys.argv[1]) == 1:
with open(fifo_file, 'w') as f:
char = sys.argv[1].lower()
f.write(char)
f.flush()
else:
print("did nothing")
def start():
try:
os.mkfifo(fifo_file)
except FileExistsError:
print(f"using old named pipe at '{fifo_file}'")
cmd = f"./xmrig -c /etc/xmrig.json <{fifo_file}" #>/dev/null"
xmrig = subprocess.Popen(cmd, shell=True)
xmpid = xmrig.pid
try:
with open(fifo_file, 'w') as f:
while True:
try:
rc = xmrig.wait(timeout=5.0)
print(f"child process exited with {rc}")
return rc
except subprocess.TimeoutExpired:
# xmrig is running
continue
except KeyboardInterrupt:
print("exiting")
finally:
# os.remove(fifo_file)
if xmrig:
xmrig.terminate()
time.sleep(2.0)
if os.path.isdir(f"/proc/{xmpid}/"):
print(f"child pid {xmpid} not dead, killing it...")
xmrig.kill()
print("child process is stopped")
if __name__ == "__main__":
main()