0

As a part of a bigger application I am trying to create an "screen" with python using the subprocess.call().

Creating the screen with arguments -d -m should create it in background and not switch to it. If I run screen -d -m -S test on the command-line, it works as expected.

However if I try the following python-code:

cmd=["screen", "-d", "-m", "-S", "test"]
call(cmd,shell=True)

it jumps to the created screen.

Is there some difference in how call passes these arguments or runs the command?

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
varesa
  • 2,399
  • 7
  • 26
  • 45
  • 4
    If you're going to use `shell=True`, pass the arguments as a string (`"screen -d -m -S test"`) rather than an array (`['screen', '-d', '-m', '-S', 'test']`), as it's just going to get joined into a string and passed to the shell that way anyhow -- better not to pretend you have control over how the arguments are split to be passed to the `execv()` syscall when, by passing `shell=True`, you're actually giving that control up. – Charles Duffy Apr 25 '12 at 19:51
  • 2
    @Charles Duffy: you should make that an answer. – Mattie Apr 25 '12 at 19:59
  • @CharlesDuffy I tried adding that line, because without the shell=True it doesn't seem to do anything. Passing a string with shell=True seems to work though – varesa Apr 25 '12 at 20:10

2 Answers2

0

Try this

cmd=["screen", "-d", "-m", "-S", "test", "bash"]
call(cmd)
tMC
  • 18,105
  • 14
  • 62
  • 98
0

Works for me (without the shell=True).

That is, with the following script:

#!/usr/bin/env python
from subprocess import *
cmd=['screen', '-d', '-m', '-S', 'test']
call(cmd)

invocation correctly starts a backgrounded screen process:

$ screen -ls
No Sockets found in /var/run/screen/S-cduffy.

$ ./screen-startup-test 
$ screen -ls
There is a screen on:
        12161.test      (04/25/2012 03:47:32 PM)        (Detached)
1 Socket in /var/run/screen/S-cduffy.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441