如何查看屏幕是否正在运行? [英] How to check screen is running?
本文介绍了如何查看屏幕是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Python中检查是否具有给定名称的屏幕.例如,检查server1是否正在运行?
How to check in Python whether the screen with the given name. For example, check if server1 is running?
谢谢:)
推荐答案
用于查找当前屏幕会话的内置命令为screen -ls
The built in command for finding current screen sessions is screen -ls
要在python中获得相同的功能,
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")
对代码的一些评论:
- 我必须使用
; true
和shell=True
,因为屏幕返回的退出代码为1
,在check_output
函数中不能很好地发挥作用. - 此外,我添加了
"."+
和+\t(
以确保我们匹配的是屏幕名称,而不是打印输出的另一部分.
- I had to use
; true
andshell=True
because screen returns a exit code of1
, which doesn't play well with thecheck_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.
这篇关于如何查看屏幕是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文