Using ulimit is the right way to solve this problem.
If the memory is exceeded and you still don’t kill it, what should you do with the program? Let it continue to use memory? Then why limit the total memory~
If the command is written by yourself, and the memory can be reclaimed as long as it is notified externally, then you can consider making this command respond to a special message, such as SIGUSR1, and reclaim the memory after receiving it. Then write a monitor program to continuously check the memory usage through /proc/$PID, and send it a message once it exceeds the limit. However, it seems that the request of the questioner is an arbitrary command, so this method obviously does not work.
Using
ulimit
is the right way to solve this problem.If the memory is exceeded and you still don’t kill it, what should you do with the program? Let it continue to use memory? Then why limit the total memory~
If the command is written by yourself, and the memory can be reclaimed as long as it is notified externally, then you can consider making this command respond to a special message, such as
SIGUSR1
, and reclaim the memory after receiving it. Then write a monitor program to continuously check the memory usage through/proc/$PID
, and send it a message once it exceeds the limit. However, it seems that the request of the questioner is an arbitrary command, so this method obviously does not work.