Ask Your Question

exec's onlyif not playing nicely with test

asked 2013-09-12 11:04:27 -0600

Rob Ogilvie gravatar image

I'm trying to execute a command if, and only if (onlyif, you might say), the clock is off. My rather unelegant solution thus far as been:

        exec { "/storage/ovm/scripts/":
                path    =>      ["/usr/bin", "/sbin", "/bin", "/usr/sbin",],
                onlyif  =>      "test \"`ntpdate -q time | awk 'NR==1{print substr($6,1,1);}'`\" != \"0\""

The awk output is 0 if the time is within a second of our time server, and nonzero if it's more than a second off. If it's more than a second off, I want the script to execute. The test command checks to see ... (more)

edit retag flag offensive close merge delete


Are you having problems with ntpd such that it isn't staying in sync with your upstream time server? I suggest troubleshooting that problem, because if you fix that you ...(more)

GregLarkin gravatar imageGregLarkin ( 2013-09-13 23:46:42 -0600 )edit

The problem is the systems boot up with a clock that is way out of sync. It's a Xen problem with standard Xen workarounds that don't work because ...(more)

Rob Ogilvie gravatar imageRob Ogilvie ( 2013-09-17 11:28:34 -0600 )edit

In that case, you could run ntpdate upon bootup to correct the large sync problem, then run ntpd as a daemon to keep it in sync from there. You would ...(more)

GregLarkin gravatar imageGregLarkin ( 2013-09-17 12:39:18 -0600 )edit

If you go that route, all you need from Puppet is a couple of service resources to manage ntpdate and ntpd.

GregLarkin gravatar imageGregLarkin ( 2013-09-17 12:39:44 -0600 )edit

Unfortunately, I have some services (such as sssd) that fail silently if the time is out of sync. So I have to manually ensure ntpdate starts far earlier than it ...(more)

Rob Ogilvie gravatar imageRob Ogilvie ( 2013-09-17 12:56:49 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-09-12 13:07:36 -0600

Rob Ogilvie gravatar image

You failed to properly escape your strings. I would suggest using single quotes to reduce the number of escapes required. In this case, the $6 is what really got you. Try this:

        exec { "/storage/ovm/scripts/":
                path    => ["/usr/bin", "/sbin", "/bin", "/usr/sbin",],
                onlyif  => 'test "`ntpdate -q time | awk \'NR==1{print substr($6,1,1);}\'`" != "0"'

....and see if it works a little better for you. Note the only escapes required were the single quotes around the awk statement.

Hope this helps!


edit flag offensive delete link more


Why yes, it did solve the problem.

Rob Ogilvie gravatar imageRob Ogilvie ( 2013-09-12 13:09:37 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2013-09-12 11:04:27 -0600

Seen: 3,156 times

Last updated: Sep 12 '13