3

This question already has an answer here:

I currently have a "friend" that has an account on my dedicated server which is running Ubuntu. Not getting into specifics, but he said he will use ~2gb max and he's using ~10gb. On to the question. Is there a way to limit his account to a maximum of say.. 3gb? Killing the process(es) would be fine too.

I've done some googling but still not found a solution :/

marked as duplicate by ire_and_curses, terdon, slm, jasonwryan, cjm Apr 9 '14 at 19:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Please edit and explain what a "dedi" is. I am assuming you mean dedicated but what are you talking about exactly? Do you have root access to this system? Do you really mean RAM or are you thinking of storage space? – terdon Apr 9 '14 at 17:24
  • Apologies, should of been more specific. By dedi i mean a dedicated server. Yes, I mean ram and yes I have full root access. – madyoda Apr 9 '14 at 17:37
  • OK, please edit your question to add extra info, it is easy to miss in the comments. Would a solution that simply kills any of his processes that uses more than N GB of RAM be acceptable? – terdon Apr 9 '14 at 17:39
  • Yeah, if any processes go over X amount of ram, kill them. And I will do, thanks :) – madyoda Apr 9 '14 at 17:48
  • 3
    The answer you are looking for is called ulimit. There is already a post about that here on this site. – Hennes Apr 9 '14 at 18:05
1

There is almost certainly a better way of doing this, probably through limits.conf but I don't know it so here's a dirty hack.

This command will kill all processes owned by the user terdon that are using more than 3G of RAM:

ps -u terdon -o vsize= -o pid= | 
    while read mem pid; do [ $mem -gt 3145728 ] && kill $pid; 
done

The ps -u terdon -o vsize= -o pid= command will print the PID and memory used (1024-byte units) by each of the processes owned by terdon. Obviously, you should adapt this to use your friend's user name. The output of the ps command is then passed through a while loop that reads the memory usage and pid and then kills the PID if it is using more than 3145728K of memory.

If you now add this as a crontab run by root, it will be run every minute and kill those processes that are using more RAM than you want to allow. So, add this line to /etc/crontab:

* * * * * root ps -u terdon -o vsize= -o pid= | while read mem pid; do [ $mem -gt 3145728 ] && kill $pid; done

As I said, this is inelegant and just an ugly hack but it might be enough.

Not the answer you're looking for? Browse other questions tagged or ask your own question.