NAME

LCFG::Utils::Service - Helper module for running service commands

VERSION

This documentation refers to LCFG::Utils::Service version 1.17.0

SYNOPSIS

use LCFG::Utils::Service;

my ( $status, $stdout, $stderr ) = 
   LCFG::Utils::Service::Run( "crond", "status" );

my $status = 
   LCFG::Utils::Service::Run( "sshd", "stop" );

LCFG::Utils::Service::Run( "httpd", "start", { timeout => 20 } );

DESCRIPTION

This module can be used to run service actions in a platform-independent way. A service is typically a daemon, such as httpd, sshd or crond, for which there are various actions which can be applied (e.g. stop, start, status). Historically in the SysVinit era these actions were called by running a script in /etc/init.d, recently this was done using the service command and now with Systemd they are executed using systemctl. It is useful to be able to call an action from a script without needing to encode any knowledge of which system is being used on a particular platform. On LCFG managed systems all the necessary information is already available in the sysinfo component resources. This module queries the sysinfo information and executes the required command in the appropriate way for the current platform.

SUBROUTINES/METHODS

Run( $service, $action, @args )

This subroutine takes the name of a service (e.g. httpd, sshd, crond) and an action you wish to execute (e.g. stop, start, reload). It will use the appropriate init tool (e.g. service or systemctl) for the current platform based on information retrieved from the LCFG sysinfo component. Optionally a list of command line arguments can be specified. It is also possible to specify values for various options by passing a reference to a hash as the final element of the @args list. The following options are supported:

timeout

This is a integer number of seconds after which if the command is still running it will be terminated. Note that if a timeout occurs the subroutine will throw an exception by calling die(). The default timeout is 600 seconds (10 minutes).

capture

This is a boolean value which can be used to disable the capturing of output going to stdout/stderr. The default behaviour is for those streams to be captured and the data returned separately. If you set this to 0 (zero) the output will not be intercepted by the Run function. Obviously, if the capturing is disabled then there will be nothing returned for the stdout and stderr variables.

sysinfo

This is a reference to an LCFG::SysInfo object which will be used to select the appropriate service command for the current platform. It is not essential to pass in this reference as a new instance will be created when necessary. The benefit of passing this reference is that it will save resources.

debug

This is a boolean value which can be set to enable extra debugging messages to be printed to stderr.

The subroutine will close file-descriptors 11 and 12 before the command is run if either of them is open. This avoids daemons inheriting file-descriptors which is a major cause of hung LCFG client processes.

If the subroutine is called in list context then 3 values will be returned. The first is the exit status. The second is the output captured from stdout and the third is the output captured from stderr. Note that if capturing is disabled then the stdout and stderr variables will be undefined. If the subroutine is called in scalar context then just the status value is returned.

Note that the subroutine does nothing to validate the service name, action name, etc. If the assembled command is invalid then when it is called the subroutine will throw an exception by calling die().

DEPENDENCIES

The commands are executed using the IPC::Run method. Exceptions are handled using the Try::Tiny module. The appropriate init tool is selected using the LCFG::SysInfo module.

SEE ALSO

lcfg-service(1)

PLATFORMS

This is the list of platforms on which we have tested this software. We expect this software to work on any Unix-like platform which is supported by Perl.

ScientificLinux6, EnterpriseLinux7, MacOSX

BUGS AND LIMITATIONS

Please report any bugs or problems (or praise!) to bugs@lcfg.org, feedback and patches are also always very welcome.

AUTHOR

Paul Anderson <dcspaul@inf.ed.ac.uk>

LICENSE AND COPYRIGHT

Copyright (C) 2015 University of Edinburgh. All rights reserved.

This library is free software; you can redistribute it and/or modify it under the terms of the GPL, version 2 or later.