Here's an idea using the process monitoring framework God. This application is written in Ruby but can be used to watch other processes, and guard against them doing things, such as dying or, in your case, use up too much RAM.
Assuming you have Ruby installed -- you can use
rvm (aka. Ruby Version Manager) to do this if you don't, but it will need to be installed and/or run as root. This is a requirement of god. You could also just install Ruby from your distro's repositories if it's available.
With a working Ruby installation you install the God gem like so:
$ [sudo] gem install god
You can then use this simple God config to do what you want.
God.watch do |w|
w.name = "hydra"
w.start = "<command to run hydra>"
w.keepalive(:memory_max => 150.megabytes,
:cpu_max => 50.percent)
Then invoke it like this:
$ god -c /path/to/simple.god -D
Now if Hydra exceeds either the CPU utilization or the memory used, God will restart it. NOTE: By default these properties will be checked every 30 seconds and will be acted upon if there is an overage for three out of any five checks.
Take a look at the documentation on God's website. The above example is from there and they do a much more thorough job of covering the details.
Another alternative is Process Resource Monitor. The feature list shows that it can monitor per process resources.
- per-process/per-user rule based resource limits
excerpt of description
Process Resource Monitor (PRM) is a CPU, Memory, Processes & Run (Elapsed) Time resource monitor for Linux & BSD. The flexibility of PRM is achieved through global scoped resource limits or rule-based per-process / per-user limits. A great deal of control of PRM is maintained through a number of ignore options, ability to configure soft/hard kill triggers, wait/recheck timings and to send kill signals to parent/children process trees. Additionally, the status output is very verbose by default with support for sending log data through syslog.
To monitor Hydra we could create a rule file like this,
# we dont care about the process run time, set value 0 to disable check
# we want to set a bit longer soft rechecks as sometimes the problem fixes
prm runs via cron,
/etc/cron.d/prm on 5 minute intervals. According to the docs this should probably be left alone.