0

I am trying to run a Python program to see if the screen program is running. If it is, then the program should not run the rest of the code. This is what I have and it's not working:

#!/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()

I know there are probably several things here that don't need to be and quite a few that I'm missing. I will be running this program in cron.

4

2 に答える 2

2
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")
于 2010-04-30T15:51:33.543 に答える
-1

おそらく、最初の os.system 呼び出しでリダイレクトするエラー メッセージは、標準出力ではなく標準エラーに書き込まれます。この行を次のように置き換えてみてください。

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

2>標準エラーをファイルにリダイレクトすることに注意してください。

于 2010-04-30T15:48:14.143 に答える