1

I am on Ubuntu 12.04 LTS running Python 2.7. My Python code looks somewhat like this:

from os import system
system("screen -S session -X stuff 'commandhere'`echo -ne '\015'`")

But when I try to run it, it does not do anything. I was wondering whether it was possible to fix this, and if so, how?

I am trying to send a command to an active screen "session" where "commandhere" is the command.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
DavidT
  • 461
  • 6
  • 17

1 Answers1

3

Have you tried subprocess.call() like this:

#!/usr/bin/python
import subprocess
subprocess.call(["screen", "-S", "session", "-X", "stuff", "'command here'`echo -ne '\015'`"])

Another idea: It might be best to just create a bash script to do the session manipulation stuff and just have Python then call the bash script.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • 1
    I tried it, but it doesn't seem to accept the "-S session" part. Specifically states "Error: Unknown option S session" – DavidT Jun 30 '14 at 01:37
  • 1
    @DavidT `-S session` is a placeholder. You need to change that to `-S [actual session number]`. With `[actual session number]` being the actual session number to be written to. – Giacomo1968 Jun 30 '14 at 01:45
  • I am using the name of the actual running session (jcmp). – DavidT Jun 30 '14 at 01:50
  • Could this be something with the python version? – DavidT Jun 30 '14 at 02:22
  • @DavidT Possibly. But issues like this are one of the reasons I avoid using Python or Ruby for calls like this. Might be best to create a `bash` script to do the session stuff and just have Python then call the `bash` script. – Giacomo1968 Jun 30 '14 at 03:13
  • 2
    it seems to be that it was an error in the call itself. I will reference the answer to [This Question](http://stackoverflow.com/questions/24482419/python-subprocess-not-executing-properly/24482434#24482434) – DavidT Jun 30 '14 at 03:16
  • @DavidT Hmmm… Interesting. Just edited my answer to address that finding. – Giacomo1968 Jun 30 '14 at 03:18