Screen get state or attach and detach in one line

I am running many separate programs at the same time screen

. Each of these exit programs ( cout

C ++) has its own current progression (from 0 to 100, 100 means they are complete). It would be easier for me if I could write a Python script that lists the individual screen sessions, then attaches and detaches them one by one, reading the progression and then outputting the whole thing to me.

I can see how to do everything except the attach / detach part. Or is there a parameter in screen

that will just return the current window?

Sorry if this sounds confusing, please feel free to ask me for clarifications.

+3


source to share


2 answers


If you're already going to use a script for monitoring, wouldn't it be much easier to run them through python and print the output?



This is fairly easy to do with a module subprocess

from the default Python library.

+1


source


This is one of the methods you could use instead of a screen. It reads a list of all programs to run from the file that 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, let me know if you need any changes and good luck!

+1


source







All Articles