Esempio n. 1
0
    def get_service(self, _id):
        """
        Get status for one specified init script.

        :param _id: Script name
        :type _id: string
        :return: Service object
        :rtype: Service
        """

        svc = Service(self)
        svc.id = svc.name = _id
        try:
            svc.running = self._run_action(_id, 'status')
            svc.state = 'running' if svc.running else 'stopped'
            svc.enabled = True
            svc.static = False
        except Exception as e:
            svc.running = False
        return svc
Esempio n. 2
0
    def list(self, units=None):
        """
        Generator of all units service in systemd.

        :param units: List of services names
        :type units: list of strings
        :return: Service object
        :rtype: Service
        """

        if not units:
            units = [x.split()[0] for x in subprocess.check_output(['systemctl', 'list-unit-files', '--no-legend', '--no-pager', '-la']).decode().splitlines() if x]
            units = [x for x in units if x.endswith('.service') and '@.service' not in x]
            units = list(set(units))

        cmd = ['systemctl', 'show', '-o', 'json', '--full', '--all'] + units

        used_names = set()
        unit = {}
        for l in subprocess.check_output(cmd).decode().splitlines() + [None]:
            if not l:
                if len(unit) > 0:
                    svc = Service(self)
                    svc.id = unit['Id']
                    svc.name, _ = svc.id.rsplit('.', 1)

                    svc.name = svc.name.replace('\\x2d', '\x2d')
                    svc.running = unit['SubState'] == 'running'
                    svc.state = 'running' if svc.running else 'stopped'
                    svc.enabled = unit['UnitFileState'] == 'enabled'
                    svc.static = unit['UnitFileState'] == 'static'

                    if svc.name not in used_names:
                        yield svc

                    used_names.add(svc.name)
                unit = {}
            elif '=' in l:
                k, v = l.split('=', 1)
                unit[k] = v