I'm running a lot of detached programs simultaneously with screen. Each of these program output (cout C++) their current progression (from 0 to 100, 100 meaning that they are completed). It would be easier for me if I could write a Python script which lists the detached screen sessions, then attaches and detaches them one by one, reading the progression and then outputting it all to me.
I can see how to do everything but the attach/detach part. Or is there a parameter in screen that would just return the current window?
Sorry if this sounds confusing, please don't hesitate asking me clarifications.
This is one method you could use instead of screen. It reads a list of all the programs you need to run from a file you pass as a command line argument.
import os
import sys
import subprocess
import threading
from queue import Queue
import time
lock = threading.Lock()
q = Queue()
def do_work(item):
while True:
line = item.stdout.readline().strip()
if line == "":
break
elif line == "100":
with lock:
print("Child process is a success.")
def worker():
item = q.get()
do_work(item)
q.task_done()
def get_programs(filename):
input = open(filename, "r")
programs = []
counter = 1
for line in input:
line = line.strip()
if line.startswith(";"):
continue
if len(line) == 0:
continue
program = line.split(" ")
if not os.path.exists(program[0]):
print("ERROR: \"%s\" at line %s does not exist." % (program[0], counter))
continue
programs.append(program)
counter += 1
return programs
if __name__ == "__main__":
if len(sys.argv) < 2 or not os.path.exists(sys.argv[1]):
print("Please provide a valid program config file.")
sys.exit()
programs = get_programs(sys.argv[1])
print("Loaded %s entries from \"%s\"." % (len(programs), sys.argv[1]))
for program in programs:
s = subprocess.Popen(program, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
q.put(s)
t = threading.Thread(target=worker)
t.daemon = True
t.start()
q.join()
print("All tasks are done.")
Hope this helps, lemme know if you need any modifications, and good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With