1.4. Runlevel Editor Library

If you need to activate or de-activate services in YaST, use module Service. It is a replacement for the old client runlevel_adjust that you should have been using before You may also use it to obtain information about services. Following functions are self-containing.

[Note]Note

You do not need to run Service::Read if you want to use any of them.

1.4.1. Enabling/Disabling Service

Service::Adjust (string name, string action) does some operation with init script. name is the name of the init script. action is one of:

  • "enable"

    enables service, which means that if service is not set to run in any runlevel, it will set it to run in default runlevels. Otherwise it will not touch the service. Use it when you want the service to run, because it preserves user-defined state. If user sets service to run in some non-default runlevels, this will not destroy her own settings.

  • "disable"

    disables service, which means that it sets it not to run in any runlevel.

  • "default"

    sets service to run in its default runlevels, which are the runlevels listed in init script comment, fiels Default-Start.

"enable" and "disable" take no action when init script links are OK, but "default" always runs insserv.

Service::Adjust ("cups", "disable");

1.4.2. Specifying Runlevels for Service

Service::Finetune (string name, list rl) sets service to run in list rl. rl is list of strings.

Service::Finetune ("cups", [ "5" ]);

Service::Finetune ("cups", [ "S", "3", "5" ]);

1.4.3. Init Script Actions

Service::RunInitScript (string name, string param) runs init script name with parameter param. Returns init scripts exit value. I do not think it is worth to import module Service only because of this one function when you may simply call SCR::Execute (.target.bash, ...) with the same result. But if you use Service for something else, this function may increase readability of the code.

Service::RunInitScript ("cups", "restart");

1.4.4. Service Info

Service::Status (string name), Service::Info (string name) and Service::FullInfo (string name)

Service::Status tells whether service runs. It calls init script status and returns exit value.

Service::Info returns information about service. The map contains:

$[ "defstart" : [ ... ], // list of strings
   "defstop"  : [ ... ], // list of default runlevels

   "start"    : [ ... ], // list of strings
   "stop"     : [ ... ], // list of real runlevels

   "reqstart" : [ ... ], // list of strings
   "reqstop"  : [ ... ], // prerequisites

   "description": string,// description
]

Values "def{start|stop}", "req{start|stop}" and "description" are taken from init script comments. "start" and "stop" are taken from links. Service::FullInfo combines info from Service::Info and Service::Status into one map. It adds key "started" with integeral value of script status. Service is enabled if "start" is not empty.

1.4.5. Is Service Enabled?

Service::Enabled (string name) returns true if service is set to run in any runlevel. False otherwise.

1.4.6. Example of Usage

import "Service";

boolean Read () {
        if (0 != Service::RunInitScript ("foo", "restart"))
        {
                y2error ("Can not start service");
                return false;
        }
        // ... your code ...
}

boolean Write () {
        // ... write settings ...

        // set service to correct state
        if (start_it)
        {
                // enable service
                Service::Adjust ("foo", "enable");
                // reload changed settings
                Service::RunInitScript ("foo", "reload");
        }
        else
        {
                // disable service
                Service::Adjust ("foo", "disable");
                // and stop it
                Service::RunInitScript ("foo", "stop");
        }
}

1.4.7. What Should You Know?

It has sense to run some services only in case that some other service runs. Now the only case known to me is portmap that is needed by NFS, NIS and others. Runlevel UI cares about this case. But if you use functions like Service::Adjust, you must take care about these dependencies yourself. If you know about other dependencies, let me know, I will implement them in UI.

Just for your info: Runleve UI warns user who wants to stop service xdm. If you know about another cases where it is wise idea to warn user, please let us know...