# Service restart based on fact

Hi,

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.

## Situation

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

## Approach

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 close merge delete ## Comments 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.. ( 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. ( 2015-11-30 03:45:58 -0500 )edit ## 2 Answers Sort by » oldest newest most voted 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/check_memory_myservice.sh', 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. more ## Comments 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](https://mmonit.com/monit/) too, since that one is new to me. ( 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. ( 2015-12-01 01:01:21 -0500 )edit You have a good point there! ;) ( 2015-12-01 07:25:43 -0500 )edit 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.

more