如何检查屏幕是否正在运行?

发布于 2024-12-13 21:46:49 字数 65 浏览 2 评论 0原文

如何在Python中检查屏幕是否具有给定的名称。例如,检查server1是否正在运行?

谢谢 : )

How to check in Python whether the screen with the given name. For example, check if server1 is running?

Thanks : )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如若梦似彩虹 2024-12-20 21:46:49

用于查找当前屏幕会话的内置命令是 screen -ls

要在 python 中获得相同的功能:

from subprocess import check_output

def screen_present(name):
        var = check_output(["screen -ls; true"],shell=True)
        if "."+name+"\t(" in var:
                print name+" is running"
        else:
                print name+" is not running"

screen_present("server1")

对代码的一些注释:

  • 我必须使用 ; trueshell=True 因为 screen 返回退出代码 1,这与 check_output 函数不能很好地配合。
  • 另外,我添加了 "."++\t( 以确保我们匹配屏幕名称而不是打印输出的其他部分。

The built in command for finding current screen sessions is screen -ls

To get the same functionality in python:

from subprocess import check_output

def screen_present(name):
        var = check_output(["screen -ls; true"],shell=True)
        if "."+name+"\t(" in var:
                print name+" is running"
        else:
                print name+" is not running"

screen_present("server1")

A couple of comments on the code:

  • I had to use ; true and shell=True because screen returns a exit code of 1, which doesn't play well with the check_output function.
  • Also, I added the "."+ and +\t( to make sure that we were matching the screen name and not another part of the printout.
赤濁 2024-12-20 21:46:49

您可以使用 subprocess 和 pgrep:

import subprocess

p = subprocess.check_output(['pgrep', '-f', 'screen'])
print p

you could use subprocess and pgrep:

import subprocess

p = subprocess.check_output(['pgrep', '-f', 'screen'])
print p
别靠近我心 2024-12-20 21:46:49

首先,你想实现什么目标?您知道您可以简单地重新连接正在运行的屏幕会话,是吗?

screen -DRS admin # creates a new session if it isn't running

同样,使用 screen -x -S admin共享 admin 会话,而无需强制分离已连接的用户。


直接回答:

您可以简单地使用列出所有正在运行的会话的输出

screen -ls

,显示它们是否也已附加:

There are screens on:
        6675.third      (11/04/2011 09:25:49 PM)        (Attached)
        6668.pts-2.koolu        (11/04/2011 09:25:38 PM)        (Attached)
        6644.admin      (11/04/2011 09:25:21 PM)        (Detached)
3 Sockets in /var/run/screen/S-sehe.

First off, what are you trying to achieve? You know that you can simply reattach a running screen session, do you?

screen -DRS admin # creates a new session if it isn't running

Likewise use screen -x -S admin to share the admin session without force-detaching the connected user(s).


Direct answer:

You can simply use the output of

screen -ls

which lists all running sessions, showing whether they are attached as well:

There are screens on:
        6675.third      (11/04/2011 09:25:49 PM)        (Attached)
        6668.pts-2.koolu        (11/04/2011 09:25:38 PM)        (Attached)
        6644.admin      (11/04/2011 09:25:21 PM)        (Detached)
3 Sockets in /var/run/screen/S-sehe.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文