簡體   English   中英

如何判斷屏幕是否正在運行?

[英]How can I tell whether screen is running?

我正在嘗試運行Python程序,以查看屏幕程序是否正在運行。 如果是這樣,則該程序不應運行其余代碼。 這是我所擁有的,並且無法正常工作:

#!/usr/bin/python

import os
var1 = os.system ('screen -r > /root/screenlog/screen.log')
fd = open("/root/screenlog/screen.log")
content = fd.readline()

while content:
 if content == "There is no screen to be resumed.":
  os.system ('/etc/init.d/tunnel.sh')
  print "The tunnel is now active."
 else:
  print "The tunnel is running."
fd.close()

我知道這里可能不需要做幾件事,而我遺漏了很多。 我將在cron中運行該程序。

from subprocess import Popen, PIPE

def screen_is_running():
    out = Popen("screen -list",shell=True,stdout=PIPE).communicate()[0]
    return not out.startswith("This room is empty")

也許您在第一個os.system調用上重定向的錯誤消息是寫在標准錯誤而不是標准輸出上的。 您應嘗試將以下行替換為:

var1 = os.system ('screen -r 2> /root/screenlog/screen.log')

注意2>將標准錯誤重定向到您的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM