0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu: /etc/rc?.d の状況を確認する

Last updated at Posted at 2023-12-10

/etc/rc N .d の状況を表示するだけの簡単なスクリプトです。

list-rc.d
#!/usr/bin/env python3

import argparse
import os
import sys


def is_ixusr(path):
    fst = os.stat(path)
    return bool(os.path.stat.S_ISREG(fst.st_mode) and
                fst.st_mode & os.path.stat.S_IXUSR)


def listdirex(path):
    return list(filter(is_ixusr, (f'{path}/{e}' for e in os.listdir(path))))


def list_init_d(init_level, etcdir):
    service = {f: [] for f in listdirex(f'{etcdir}/init.d')}
    flist = ([os.path.realpath(f), [l, f]]
             for l in init_level for f in listdirex(f'{etcdir}/rc{l}.d'))
    for real, (level, link) in filter(lambda d: d[0] in service, flist):
        service[real].append([level, os.path.basename(link)])
    return tuple(sorted((os.path.basename(name), tuple(sorted(mode)))
                        for name, mode in service.items()))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', action='store_true', default=False)
    parser.add_argument('-e', '--etc', metavar='DIR', dest='etc_dir', default='/etc')
    parser.add_argument('-l', '--level', metavar='INIT', default='0123456S')
    parser.add_argument('services', metavar='service', nargs='*')
    args = parser.parse_args()

    verbose = args.verbose
    init_level = args.level
    etc_dir = args.etc_dir
    services = args.services

    init_list = list_init_d(init_level, etc_dir)
    initd = init_list if not services else tuple(
        d for d in init_list if d[0] in services)
    wname = max([0, *[len(n) for n, _ in initd]])
    padding = ' ' * wname
    for name, mode in initd:
        mon = {l: 'off' for l in init_level}
        mon |= {l[0]: 'on ' for l in filter(
            lambda s: s[1][0] == 'S', mode)}
        mon = '  '.join(f'{k}:{mon[k]}' for k in init_level)
        print(f'{(name+padding)[:wname]}  {mon}')
        if verbose:
            msg = '\n'.join(f'{padding}    rc{l}.d/{f}'
                            for l, f in mode if l in init_level)
            print(msg + '\n' if msg else '')


main()
実行結果
$ list-rc.d postfix kmod
kmod     0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
postfix  0:off  1:off  2:on   3:on   4:on   5:on   6:off  S:off
$ list-rc.d postfix kmod -v
kmod     0:off  1:off  2:off  3:off  4:off  5:off  6:off  S:on 
           rcS.d/S01kmod

postfix  0:off  1:off  2:on   3:on   4:on   5:on   6:off  S:off
           rc0.d/K01postfix
           rc1.d/K01postfix
           rc2.d/S01postfix
           rc3.d/S01postfix
           rc4.d/S01postfix
           rc5.d/S01postfix
           rc6.d/K01postfix
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?