Running shell commands with Cfengine 3

Although it is a very bad idea to directly run shell commands with Cfengine 3 (see here why), there might be a few situations where this is still the best attempt, e.g. when performing custom maintenance work which really can not be done by Cfengine 3.

Writing the Cfengine 3 code

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

bundle agent run_shell_command {
        "/etc/init.d/mysql start"
        comment => "Doing it this way is a really bad idea!";

Save the file (I used the file name “”) and make sure that it is located in the right directory (e.g. /var/lib/cfengine3/inputs or /etc/cfengine3).
Let’s see if the syntax is correct:

cf-promises -f /var/lib/cfengine/inputs/

No output or error message? Great, let’s run it then:

cf-agent -f /var/lib/cfengine3/inputs/
Q: " star": Rather than invoking init scripts through /etc/init.d, use the service(8)
Q: " star": utility, e.g. service mysql start
Q: " star": Since the script you are attempting to invoke has been converted to an
Q: " star": Upstart job, you may also use the start(8) utility, e.g. start mysql
Q: " star": mysql start/running, process 4521
I: Last 5 quoted lines were generated by promiser "/etc/init.d/mysql start"

As you can see, the cf-agent is able to run our simple shell command and also shows you its output. As stated above, it is a really bad idea to start services in this way. Cfengine 3 is able to do that for you, but we just needed a simple example here.

Analyzing the Cfengine 3 code snippet
We jump directly to the interesting part:

bundle agent run_shell_command {
        "/etc/init.d/mysql start"
        comment => "Doing it this way is a really bad idea!";

Inside our bundle agent “run_shell_command”, a promise of the type “command” is created. This promise simply contains the command to be performed and a small comment. That’s it!

As usual, you can download todays’s Cfengine 3 code snippet here.

3 thoughts on “Running shell commands with Cfengine 3

  1. Hey Aleksey,
    nice reading from you. I read your blog from time to time and stumbled across your Cfengine 3 videos.

    I use WordPress as a blog system and “Qoate Simple Code Snippets” for the syntax highlighting. Hope that helps!

Comments are closed.