1.设置监控,当某个进程(monitor_sure_onlyme)不在时就自动把它拉起来

#! /bin/bash

while [ 1 ]
do
  ranktest=`ps aux | grep rank_test_sure_onlyme | grep -v "grep"`
  if [ "$ranktest" == "" ]; then
    #echo ranktest not exist
    nohup /home/admin/cpuuse/rank_test_sure_onlyme >> /dev/null 2>&1 &
  fi
  sleep 60
done

2.占用内存的脚本(C++)
(1)RunTempThread,是为了每天7:30~8:30多占资源的
(2)while(1)是死循环,会占掉一个cpu核心
(3)正常情况下,一直是cpu 300%,就是因为main里的这个for循环,启动了三个线程
(4)后来又启动了12个,到7:30的时候,就会再多启动12个线程,会占到1500%

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int begintime = 730;
const int endtime = 830;
const long long global_gsize = 1024 * 1024 * 1024;

int GetCurTime()
{
    time_t timep;
    time(&timep);
    struct tm *now = localtime(&timep);
    return 100 * (now->tm_hour) + now->tm_min;
}

//RunTempThread,是为了每天7:30~8:30多占资源的
void RunTempThread(int count)
{
    atomic running(false);
    while (1)
    {
        int curtime = GetCurTime();
        if (!running && curtime > begintime && curtime < endtime)
        {
            running = true;
            for (size_t i = 0; i < count; i++)
            {
                thread th([&running] {
                    long long memsize = global_gsize;
                    char *ptest = new char[memsize];
                    memset(ptest, 1, memsize); // 比较耗时,多线程申请内存;
                    while (running)
                    {
                    };
                    delete[] ptest;
                });
                th.detach();
            }
        }
        if (running && curtime > endtime)
        {
            running = false;
        }

        this_thread::sleep_for(chrono::seconds(20));
    }
}

int main()
{
    int num = 3;
    for(int i=0;i < num; ++i)
    {
        thread th([] {
            long long memsize = 4 * global_gsize;
            char *ptest = new char[memsize];
            memset(ptest, 1, memsize);      // 比较耗时,多线程申请内存;
            while (1)
            {
            }
            delete[] ptest;
        });
        th.detach();
    }

    //getchar();
    RunTempThread(12);
    return 0;
}

2.对比两个文件夹的差异
会生成两个MD5值,对比md5值即可。

#!/bin/bash
echo $#
[ "$#" -lt "2" ] && echo "$0 base_folder cmp_folder" && exit
base_folder=$1
cmp_folder=$2
#base_file_list=$(tree -fi $base_folder)
#cmp_file_list=$(tree -fi $cmp_folder)
base_file_list=$(find $base_folder -type f)
cmp_file_list=$(find $cmp_folder -type f)
rm base_md5.list cmp_md5.list
for i in $base_file_list;do
    #md5sum $i >> base_md5.list
    echo -e "${i#*/}\t\c" >> base_md5.list
    ls -al $i|cut -d " " -f 1,3,4,5 >> base_md5.list
done
for i in $cmp_file_list;do
    #md5sum $i >> cmp_md5.list
    echo -e "${i#*/}\t\c" >> cmp_md5.list
    ls -al $i|cut -d " " -f 1,3,4,5 >> cmp_md5.list
done