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.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
spxxn
  • 11
  • 2

2 Answers2

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")
sastanin
  • 40,473
  • 13
  • 103
  • 130
  • Tried this as well and had no luck. – spxxn Apr 30 '10 at 16:23
  • Could you post your error message? What version of Python you are using? Is it possible that screen is not installed where you run it? – sastanin Apr 30 '10 at 16:43
  • Don't forget, the output of `screen -list` depends on the value of the NETHACK option. :) @spxxn: what is the output of `screen -list` – MikeyB Apr 30 '10 at 18:07
-1

Maybe the error message that you redirect on the first os.system call is written on the standard error instead of the standard output. You should try replacing this line with:

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

Note the 2> to redirect standard error to your file.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
  • It is writing it to the file. When I try to redirect it, it writes no info to the file. – spxxn Apr 30 '10 at 16:22