Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Need Script to reboot server if memory usage go above 95%
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Need Script to reboot server if memory usage go above 95%

mca295188mca295188 Member
edited September 2013 in Help

Hello All,

I have VPS with 1GB RAM & 1GB VSwap..

i just need some kind of script which will first stop the apache

and then reboot the server...if both RAM and VSwap usage is above 95%

please someone help me....

Comments

  • @Zen

    Here is the output of top command in my VPS

    now its like this..

    script should find out Free RAM deducting cached usage also..

    and if RAM is 95% used then it should do the same thing with Swap

    and if Swap is 90% used...then it should stop apache and reboot VPS...

    by the way thanks for providing the current script...

  • @Zen

    Many Many Thanks for your help.....

  • agentmishraagentmishra Member, Host Rep

    nice thread

    and nice is the script

  • @Zen

    Hey i have created my script now...
    with calculating actual available RAM (cache memory is also calculated)
    and its working good enough for me..
    and many many thanks again to you...
    because i created mine only getting the idea from yours...

  • This is the wrong way to go about managing a server.

    You should configure limits to the number of simultaneous apache processes that can be created, based on the amount of RAM you have. Then, when you have a traffic spike, some people will have to wait in queue for an available apache process. If this happens a lot then you need more RAM (or a more memory-efficient webserver stack). It it happens only occasionally then you might choose to live with it.

    Rebooting to free up memory is very Windows 95.

    Thanked by 1raindog308
  • But... What if I'm running Windows 95 on my VPS?

  • netomxnetomx Moderator, Veteran

    @Magiobiwan said:
    But... What if I'm running Windows 95 on my VPS?

    wait, are you sonwebhost?

    Thanked by 1raindog308
  • Most definitely not.

  • Use monit for this, it is easily configurable and you can have it restart the processes instead of restarting the server.

    Monit

    apt-get install monit (Debian)

    How to install monit on CentOS/Debian/Ubuntu

    Cheers!

  • I'd second sleddog, but otherwise use monit. You can set it to escalate first with alerts and possibly restarting apache. Rebooting server wide isn't what it does, but it might be better suited to control runaway processes.

    http://viktorpetersson.com/2010/07/09/setting-up-monit-to-monitor-apache-and-postgresql-on-ubuntu/

    gives a good example of using it for apache.

  • @Magiobiwan said:
    But... What if I'm running Windows 95 on my VPS?

    Then you'll probably need to reboot occasionally to free memory. I thought I was clear on that.

  • If I were you I'd use nginx or something with a much smaller memory print. Add php-fpm to it and set it to run processes ondemand and until someone actually views the sites you won't be using any memory but the small amount nginx needs to run (depending on how many children you set it to use, 1 per processor/thread is a good rule for that). Nice script though provided by @Zen. Is it a good idea to include cached ram though since linux auto caches a lot of stuff to reduce the servers workload. Wouldn't it be better to exclude cached ram and just go to reboot with real mem use at 95%?

  • @sleddog

    Yeah i know but my client scripts sometimes leave apache processes and those processes can not be used again...and he is working on his scripts...so this script is for short time duration..

    @Magiobiwan

    Windows 95....i think that's forbidden to use now a days...LOL

    @TheLinuxBug

    Thanks but i think that will consumer resources na...

    @tchen

    i have already modified the script to only kill process not to reboot vps

    @sc754

    I don't know anything about what my clients scripts need..

    so i am not sure about using nginx

  • @mca295188 said:
    sleddog

    Yeah i know but my client scripts sometimes leave apache processes and those processes can not be used again...and he is working on his scripts...so this script is for short time duration..

    Magiobiwan

    Windows 95....i think that's forbidden to use now a days...LOL

    TheLinuxBug

    Thanks but i think that will consumer resources na...

    tchen

    i have already modified the script to only kill process not to reboot vps

    sc754

    I don't know anything about what my clients scripts need..

    so i am not sure about using nginx

    Um, unless its watching a ton of programs it may use 1-2mb of ram if that. I think that is an easy enough trade off to restart Apache and/or do what you are wanting to prevent the server from crashing. Also it is highly configurable, you can set it based on ram, % of cpu used, etc, etc. At least look at it before your rule it out. Not sure why you think it will "consumer resources".

    Cheers!

  • use debian + nginx, you will consume only a little RAM. for 512mb OVZ, never to worry about that. grab one of those $15 per year 512RAM OVZ VPS

  • @hostingtalking said:
    use debian + nginx, you will consume only a little RAM. for 512mb OVZ, never to worry about that. grab one of those $15 per year 512RAM OVZ VPS

    You are the scum of the earth.

  • @awson said:
    You are the scum of the earth.

    Find a negative emotion engine!

  • @TheLinuxBug

    i am not ruling it out...

    actually i don't know anything about that...

  • @mca295188 Why would it be forbidden to use Windows 95? I have a valid license for it. It's gone EOL for patches (has been for a WHILE) but that doesn't mean you can't still RUN it.

  • mca295188mca295188 Member
    edited September 2013

    @Magiobiwan

    Sir,

    actually that's very old na so i was just joking about that forbidden thing...

    everyone has right to use any OS they want to....

  • xiaowazixiaowazi Member
    edited March 29

    `#!/bin/bash

    设置阈值

    CPU_THRESHOLD=95
    MEM_THRESHOLD=95

    获取当前CPU和内存使用率

    get_usage() {
    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{printf("%.1f\n", $2 + $4)}')
    MEM_USAGE=$(free | awk '/Mem:/ {printf "%d", $3/$2*100}')
    }

    重启服务器

    reboot_server() {
    logger "CPU usage ${CPU_USAGE}% or memory usage ${MEM_USAGE}% exceeded threshold. Rebooting server..."
    reboot
    }

    主循环

    while true; do
    get_usage

    # 检查CPU和内存使用率是否超过阈值
    if [ $(echo "$CPU_USAGE > $CPU_THRESHOLD" | bc -l) -eq 1 ] || [ "$MEM_USAGE" -ge "$MEM_THRESHOLD" ]; then
        reboot_server
    fi
    
    sleep 60
    

    done

    将脚本加入到系统启动服务

    if [ -d "/etc/init.d" ]; then
    sudo cp "$0" /etc/init.d/vps-monitor
    sudo chmod +x /etc/init.d/vps-monitor
    sudo update-rc.d vps-monitor defaults
    elif [ -d "/etc/systemd/system" ]; then
    sudo cp "$0" /etc/systemd/system/vps-monitor.service
    sudo chmod 644 /etc/systemd/system/vps-monitor.service
    sudo systemctl enable vps-monitor.service
    fi
    `

  • xvpsxvps Member
    edited March 29

    Impressive. It only took you teen years to program a solution.

    What was the breakthrough? ChatGPT?

Sign In or Register to comment.