2

I am not sure if it is even possible, but can I somehow restart service when memory has been already used over 90%?

I mean script will check memory, and if memory has been used over 90% it will restart/execute restart of my service?

Delirium
  • 207
  • 4
  • 11

2 Answers2

3

Have a look at SystemD's resource-control.

Combining MemoryMax and Restart=always should do the trick.

fuero
  • 9,591
  • 1
  • 35
  • 40
  • I was looking more for something like "if memory usage is over 90% restart this service" I dont know how to parse memory usage in percentege and use it as variable. – Delirium Apr 06 '20 at 09:22
  • 1
    Um... calculate it with bc? If you use puppet or ansible you can do it with a template + fact. – fuero Apr 06 '20 at 10:04
  • The problem with that is that it uses the oom killer, so it doesn't do a clean shutdown. What I want is, if the process exceeds a certain amount of memory, trigger a clean restart (that is, run the stop action, then run the start action). – Thayne Jan 06 '23 at 18:10
2

Wrote own script in the end

m=`free -m | head -n value | tail -n 1 | tr -s " " | cut -d " " -f value`
if [ $m -ge 10000 ]; then
  systemctl restart service
fi
Delirium
  • 207
  • 4
  • 11