Python Forum
a class that gets available, running, and stopped services (Ubuntu/Debian)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a class that gets available, running, and stopped services (Ubuntu/Debian)
#2
I would rather return a sequence of small Service objects for such a program, because Python is very strong at manipulating collections and I think it opens more flexible possibilities. Here is a proposal
from collections import namedtuple
import os

class Service(namedtuple('Service', 'name status')):
    __slots__ = ()
    def is_running(self):
        return self.status == '+'
    def is_stopped(self):
        return self.status == '-'
    def has_no_status(self):
        return self.status == '?'

def service_status_all():
    for line in os.popen("service --status-all &> /dev/null"):
        _, status, _, name = line.strip().split()
        yield Service(name, status)
        
if __name__ == '__main__':
    services = list(service_status_all())
    print("{} services are available".format(len(services)))
    for x in services:
        print(x.name)
    running = [s for s in services if s.is_running()]
    print()
    print("{} services are running".format(len(running)))
    for y in running:
        print(y.name)
    stopped = [s for s in services if s.is_stopped()]
    print()
    print("{} services are stopped".format(len(stopped)))
    for y in stopped:
        print(y.name)
Reply


Messages In This Thread
RE: a class that gets available, running, and stopped services (Ubuntu/Debian) - by Gribouillis - Aug-25-2018, 06:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A torsocks DDOS tool for Debian Linux Distros: python + wget + tor rootVIII 0 2,393 Jun-09-2019, 09:53 AM
Last Post: rootVIII
  A small/simple class for running nslookup on a list of domain names rootVIII 1 3,564 Apr-17-2019, 04:49 AM
Last Post: rootVIII

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020