2

How do I read a pid file in cfengine 3 and restart that process if it isn't running?

2

In cfengine 2, I used something like:

processes:
  "httpd" restart "/etc/init.d/apache restart"

In cfengine 3, restarting of processes must be coded as a separate commands.

processes:
   "httpd"
     restart_class => "start_httpd";

commands:
  start_httpd::
    "/etc/init.d/apache restart";

This will define the class "start_httpd" if httpd is not running. Then will run the init script to make sure it starts. See the cf3 reference manual for a deeper explanation of how the restart_class works.

1

Instead of looking at the PID file directly, I'd try to let Cfengine 3 manage your selected service/process. For Cfengine 3 you can use the following code (it might not be perfect, but it works):

body common control {
    version => "1.0";
    bundlesequence => { "check_services" };
}

bundle agent check_services {
    vars:
        "services" slist => { "apache2", "mysql" };
        "init_scripts_path" string => "/etc/init.d"; 

    processes:
        "$(services)"
            comment => "Check if the processes for '$(services)'",
            restart_class => "restart_$(services)";

    commands:
        "${init_scripts_path}/${services} start"
            comment => "Restarting the service",
            ifvarclass => "restart_${services}";

}

Please note that I wrote this Cfengine 3 script for an Ubuntu client, so you might have to adapt it to your needs and distribution.

You can download this code snippet here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.