Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check screen is running?

How to check in Python whether the screen with the given name. For example, check if server1 is running?

Thanks : )

like image 492
user1030344 Avatar asked Dec 10 '25 18:12

user1030344


2 Answers

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:

  • I had to use ; true and shell=True because screen returns a exit code of 1, which doesn't play well with the check_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.
like image 137
Peter Olson Avatar answered Dec 12 '25 08:12

Peter Olson


you could use subprocess and pgrep:

import subprocess

p = subprocess.check_output(['pgrep', '-f', 'screen'])
print p
like image 23
Matt Sweeney Avatar answered Dec 12 '25 09:12

Matt Sweeney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!