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 :/

  • 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

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; 

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.

| improve this answer | |

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