How to check in Python whether the screen with the given name. For example, check if server1 is running?
Thanks : )
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:
; true and shell=True because screen returns a exit code of 1, which doesn't play well with the check_output function."."+ and +\t( to make sure that we were matching the screen name and not another part of the printout.you could use subprocess and pgrep:
import subprocess
p = subprocess.check_output(['pgrep', '-f', 'screen'])
print p
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With