每天随机时间重启服务

crontab

# Restart ats in random time of every day to release swap.
0 2 * * * root /bin/sh /opt/soft/vcdn_tools/restart_ats.sh > /dev/null 2>&1

shell

#!/bin/bash
# Author      : 71standby@gmail.com
# Date        : 2018-03-02
# Description : Restart ats in random time of every day to release swap. 

function random_sleep()
{
    randNum=$(($RANDOM%60))
    echo "Now to sleep $randNum minutes..." >> "/data/ats/logs/swap_release.log"
    sleep $randNum"m"
}

function ats_statistics()
{
    SUM=0
    OVERALL=0
    for PID in `ps -ef |grep traffic_ |grep -v grep |grep -v $0 |awk '{print $2}'`
    do
        # PROGNAME=`ps -p $PID -o comm --no-headers`
        DIR=/proc/$PID/smaps
        for SWAP in `grep Swap $DIR 2>/dev/null| awk '{ print $2 }'`
        do
            let SUM=$SUM+$SWAP
        done
        # echo "PID=$PID - Swap used: $SUM - ($PROGNAME)"
        let OVERALL=$OVERALL+$SUM
        SUM=0
    done
    current=`date "+%Y-%m-%d %H:%M:%S"`
    echo $current >> "/data/ats/logs/swap_release.log"
    echo "ATS swap used: $OVERALL" >> "/data/ats/logs/swap_release.log"
}

echo "############## start ##############" >> "/data/ats/logs/swap_release.log"
ats_statistics
random_sleep
systemctl restart ats 2>/dev/null || /etc/init.d/ats restart
ats_statistics
echo "##############  end  ##############" >> "/data/ats/logs/swap_release.log"

  

posted @ 2018-03-05 11:09  lixin[at]hitwh  阅读(251)  评论(0编辑  收藏  举报