(R)?ex Deployment & Configuration Management

Search

News

2016-09-08

Work with (R)?ex at adjust! Check out their job posting for details, or contact our fellow core developer, FErki.

2016-07-16

(R)?ex 1.4.1 released, fixing many bugs. See the release notes for more details.

2016-03-07

(R)?ex 1.4.0 released, containing lots of goodies. See the release notes for more details.

2015-09-04

(R)?ex 1.3.3 released, fixing a bunch of bugs. See the release notes for more details.

2015-06-22

adjust GmbH is sponsoring the (R)?ex project with a new build server. Thank you for your support!

2015-06-17

(R)?ex 1.3.2 released, fixing issues related to file manipulation when run on BSDs. See the release notes for more details.

2015-06-08

(R)?ex 1.3.1 released, fixing Rex::Commands::DB related tests.

2015-05-27

Read the second part of "Rex in practice" series about Test-driven infrastructure.

2015-05-09

repositor.io 1.1.0 released. repositor.io is a repository management tool for apt, yum, yast and docker. This is a bugfix release with fixes for ubuntu and centos7 installation media mirroring.

2015-05-03

(R)?ex 1.2.0 released. See the release notes for more details.

2015-03-27

View the slides of the talk An introduction to Rex from Andy Beverley.

Conferences

2016-06-21

Training

Need Help?

Rex is a pure open source project, you can find community support in the following places:

Professional support is also available.

» Home » Docs » API » 1.4 » Rex » Commands » Service.pm

Service.pm

API Version:

NAME

Rex::Commands::Service - Manage System Services

DESCRIPTION

With this module you can manage Linux services.

SYNOPSIS

 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";

EXPORTED FUNCTIONS

service($service, $action, [$option])

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

starting a service
 task "start-service", "server01", sub {
   service apache2 => "start";
 };
stopping a service
 task "stop-service", "server01", sub {
   service apache2 => "stop";
 };
restarting a service
 task "restart-service", "server01", sub {
   service apache2 => "restart";
 };
checking status of a service
 task "status-service", "server01", sub {
   if( service apache2 => "status" ) {
     say "Apache2 is running";
   }
   else {
     say "Apache2 is not running";
   }
 };
reloading a service
 task "reload-service", "server01", sub {
   service apache2 => "reload";
 };
ensure that a service will started at boot time
 task "prepare", sub {
   service "apache2",
     ensure => "started";
 };
ensure that a service will NOT be 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:

before_action

For example: before_start, before_stop, before_restart

This gets executed right before the service action.

after_action

For example: after_start, after_stop, after_restart

This gets executed right after the service action.

service_provider_for $os => $type;

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).

Fork me on GitHub
Google Group / Twitter / GitHub / Mailinglist / irc.freenode.net #rex   -.รด.-   Disclaimer