DOKK / manpages / debian 10 / rex / Rex::Commands::Service.3pm.en
Rex::Commands::Service(3pm) User Contributed Perl Documentation Rex::Commands::Service(3pm)

Rex::Commands::Service - Manage System Services

With this module you can manage Linux services.

 use Rex::Commands::Service
 service apache2 => "start";
 service apache2 => "stop";
 service apache2 => "restart";
 service apache2 => "status";
 service apache2 => "reload";
 service apache2 => "ensure", "started";
 service apache2 => "ensure", "stopped";

The service function accepts 2 parameters. The first is the service name and the second the action you want to perform.

 task "start-service", "server01", sub {
   service apache2 => "start";
 };
    
 task "stop-service", "server01", sub {
   service apache2 => "stop";
 };
    
 task "restart-service", "server01", sub {
   service apache2 => "restart";
 };
    
 task "status-service", "server01", sub {
   if( service apache2 => "status" ) {
     say "Apache2 is running";
   }
   else {
     say "Apache2 is not running";
   }
 };
    
 task "reload-service", "server01", sub {
   service apache2 => "reload";
 };
    
 task "prepare", sub {
   service "apache2",
     ensure => "started";
 };
    
 task "prepare", sub {
   service "apache2",
     ensure => "stopped";
 };
    

If you need to define a custom command for start, stop, restart, reload or status you can do this with the corresponding options.

 task "prepare", sub {
   service "apache2",
     ensure  => "started",
     start   => "/usr/local/bin/httpd -f /etc/my/httpd.conf",
     stop    => "killall httpd",
     status  => "ps -efww | grep httpd",
     restart => "killall httpd && /usr/local/bin/httpd -f /etc/my/httpd.conf",
     reload  => "killall httpd && /usr/local/bin/httpd -f /etc/my/httpd.conf";
 };
    

This function supports the following hooks:

For example: before_start, before_stop, before_restart

This gets executed right before the service action.

For example: after_start, after_stop, after_restart

This gets executed right after the service action.

To set another service provider as the default, use this function.

 user "root";
 group "db" => "db[01..10]";
 service_provider_for SunOS => "svcadm";
 task "start", group => "db", sub {
    service ssh => "restart";
 };

This example will restart the ssh service via svcadm (but only on SunOS, on other operating systems it will use the default).

2018-02-01 perl v5.26.1