【问题标题】:Unexpected result with gnu-screen and call()gnu-screen 和 call() 的意外结果
【发布时间】:2012-04-25 19:49:30
【问题描述】:

作为更大应用程序的一部分,我正在尝试使用 subprocess.call() 使用 python 创建一个“屏幕”。

使用参数-d -m 创建屏幕应该在后台创建它而不是切换到它。如果我在命令行上运行 screen -d -m -S test,它会按预期工作。

但是,如果我尝试以下 python 代码:

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

它跳转到创建的屏幕。

调用传递这些参数或运行命令的方式有什么不同吗?

【问题讨论】:

  • 如果您要使用shell=True,请将参数作为字符串 ("screen -d -m -S test") 而不是数组 (['screen', '-d', '-m', '-S', 'test']) 传递,因为它只是要加入到字符串中并以这种方式传递给 shell - 最好不要假装你可以控制如何拆分参数以传递给execv() 系统调用,而通过传递shell=True,你实际上放弃了控制。
  • @Charles Duffy:你应该这样回答。
  • @CharlesDuffy 我尝试添加该行,因为没有 shell=True 它似乎没有做任何事情。使用 shell=True 传递字符串似乎可行

标签: python subprocess gnu-screen


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    为我工作(没有shell=True)。

    也就是说,使用以下脚本:

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

    调用正确启动后台屏幕进程:

    $ 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-03
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 2021-09-07
      • 2017-04-24
      相关资源
      最近更新 更多