Sebastian Karlsson
Sebastian Karlsson

Reputation: 745

Starting a screen and executing a bash command in Python

So in Python, I am trying to create a unix screen and have it execute a command but it does not work. The screen isn't event created and I can't see it if I run screen -ls

This is the code:

def run_screen_with_command(command):
    screens = []

    command = ['screen', '-d', '-m', '-S', 'myscreen', 'bash', '-c', "'{}'".format(command)] 
    process = subprocess.Popen(command,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT)

    process.wait()
    print(process.stderr, process.stdout.read())
    #returncode = process.wait()
    print ' '.join(command)

    return process


run_screen_with_command('sleep 10')

Upvotes: 1

Views: 351

Answers (1)

chepner
chepner

Reputation: 531175

You don't need to quote the argument for bash -c, since the shell isn't executing it.

command = ['screen', '-d', '-m', '-S', 'myscreen', 'bash', '-c', command]

Upvotes: 2

Related Questions