Ask Your Question

Service restart based on fact

asked 2015-11-27 06:35:37 -0500

asktbt gravatar image


any idea how I can trigger a service restart based on a fact without using the exec-resource? Following the mantra "if you're using exec, you're doing something wrong." I wonder how to do that.


I have got a process that occasionally takes too much memory. The approved fix for this is to restart the involved service.


I plan to gather the memory-usage in a fact and based on the value to restart the service if necessary.
Without writing something like:

if $fact > <integer> {
  exec {'restart-service':
    command => 'systemctl restart <servicename>

I wonder if I can take usage of the notify-attribute somehow... Any ideas?

edit retag flag offensive close merge delete


i dont think this is really something that should be done via puppet - as ramindk suggested monit would be good - or fixing app so that it doesnt have a memory leak..

sahumphries gravatar imagesahumphries ( 2015-11-29 06:00:15 -0500 )edit

Yes, I understand. I am not in the situation where I can expect a fix for this application anytime soon. The only approach to improve the situation on my side is to implement this. I have basically now implemented a fact counting the memory usage and trigger a service restart via Exec. Thanks.

asktbt gravatar imageasktbt ( 2015-11-30 03:45:58 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2015-11-27 17:43:16 -0500

ramindk gravatar image

You might do something like the following. It is an exec, but it's only gathering the information to notify the service.

exec { 'check_memory_myservice':
   command => '/usr/bin/logger -t check_memory: "restarting myservice due to memory',
   onlyif  => '/usr/bin/',
   notify  => Service['myserver']

Your script would need to exit with 0 in order for the command to run and the notify to happen.

However it might be better to use Puppet to configure Monit or similar tools to do this instead.

edit flag offensive delete link more


Thanks for that approach. I do not see the benefit or disadvantage of using a script instead of a fact though. I will have a look into [Monit]( too, since that one is new to me.

asktbt gravatar imageasktbt ( 2015-11-30 03:47:45 -0500 )edit

You could use a fact, but all facts are executed on all machines regardless of whether the fact is required in the catalog. I'd prefer an Exec in this case because it's more targeted. The example above will also only run if the conditions match so it won't pollute your logs/reports.

ramindk gravatar imageramindk ( 2015-12-01 01:01:21 -0500 )edit

You have a good point there! ;)

asktbt gravatar imageasktbt ( 2015-12-01 07:25:43 -0500 )edit

answered 2015-11-29 07:58:35 -0500

You could do the following in your manifest:

$culprit = 'myservice'
$threshold = 80
if (($::memory['used_bytes']/$::memory['total_bytes'])*100) > $threshold {
  notify { "System memory is over ${threshold} percent usage, restarting ${culprit}":
    notify => Service[$culprit],

This would also assume you have the service resource for 'myservice' managed, and that you're using facter 3, but if you wrote a custom fact for the memory you could have it just output a boolean and make it:

if $myfact {}

This is far from ideal and you'd want to address the underlying reason that the service is filling the memory, but something like this should work without the overhead of an exec.

edit flag offensive delete link more

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: 2015-11-27 06:35:37 -0500

Seen: 1,909 times

Last updated: Nov 27 '15