linux 重启服务器脚本

If you have been an admin for any length of time, you have certainly discovered situations where a server spikes in CPU use or memory utilization and/or load levels. Running `top` won’t always give you the answer, either. So how do you find those sneaky processes that are chewing up your system resources to be able to kill ’em?

如果您曾经担任管理员一段时间,则肯定会发现服务器CPU使用率或内存利用率和/或负载水平激增的情况。 运行“ top”也不会总是给您答案。 那么,如何找到那些消耗系统资源以杀死它们的偷偷摸摸的进程呢?

The following script might be able to help. It was written for a web server, so has some parts of it that are specifically looking for httpd processes and some parts that deal with MySQL. Depending on your server deployment, simply comment/delete those sections and add others. It should be used for a starting point.

以下脚本可能会有所帮助。 它是为Web服务器编写的,因此其中的某些部分专门寻找httpd进程,而某些部分则处理MySQL。 根据您的服务器部署,只需注释/删除这些部分并添加其他部分。 它应该用作起点。

Prerequisites for this version of the script is some freeware released under the GNU General Public License called mytop (available at http://jeremy.zawodny.com/mysql/mytop/) which is a fantastic tool for checking how MySQL is performing. It is getting old, but still works great for our purposes here. Additionally, I use mutt as the mailer – you may want to change the script to simply use the linux built in `mail` utility. I run it via cron every hour; adjust as you see fit. Oh – and this script needs to run as root since it does read from some protected areas of the server.

此脚本版本的前提条件是一些免费软件在GNU通用公共许可证下发行,称为mytop(可从http://jeremy.zawodny.com/mysql/mytop/获得 ),这是一种用于检查MySQL的性能的出色工具。 它已经变老了,但对于我们这里的目的仍然很有效。 另外,我使用mutt作为邮件程序–您可能希望更改脚本以仅使用内置于`mail`实用程序的linux。 我每小时通过cron运行一次; 视需要调整。 哦-该脚本需要以root用户身份运行,因为它确实从服务器的某些受保护区域读取。

So let’s get started, shall we?

那么,让我们开始吧?

First, set your script variables:

首先,设置脚本变量:

#!/bin/bash
#
# Script to check system load average levels to try to determine
# what processes are taking it overly high...
#
# 07Jul2010 tjones
#
# set environment
dt=`date +%d%b%Y-%X`
# Obviously, change the following directories to where your log files actually are kept
tmpfile="/tmp/checkSystemLoad.tmp"
logfile="/tmp/checkSystemLoad.log"
msgLog="/var/log/messages"
mysqlLog="/var/log/mysqld.log"
# the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report)
mailstop="sysadmin@mydomain.com"
mailstop1="15555555555@mycellphone.com"
machine=`hostname`
# The following three are for mytop use - use a db user that has decent rights
dbusr="username"
dbpw="password"
db="yourdatabasename"
# The following is the load level to check on - 10 is really high, so you might want to lower it.
levelToCheck=10

#!/bin/bash
#
# Script to check system load average levels to try to determine
# what processes are taking it overly high...
#
# 07Jul2010 tjones
#
# set environment
dt=`date +%d%b%Y-%X`
# Obviously, change the following directories to where your log files actually are kept
tmpfile="/tmp/checkSystemLoad.tmp"
logfile="/tmp/checkSystemLoad.log"
msgLog="/var/log/messages"
mysqlLog="/var/log/mysqld.log"
# the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report)
mailstop="sysadmin@mydomain.com"
mailstop1="15555555555@mycellphone.com"
machine=`hostname`
# The following three are for mytop use - use a db user that has decent rights
dbusr="username"
dbpw="password"
db="yourdatabasename"
# The following is the load level to check on - 10 is really high, so you might want to lower it.
levelToCheck=10

Next, check your load level to see if the script should continue:

接下来,检查您的负载级别以查看脚本是否应该继续:

# Set variables from system:
loadLevel=`cat /proc/loadavg | awk '{print $1}'`
loadLevel=$( printf "%0.f" $loadLevel )

# Set variables from system:
loadLevel=`cat /proc/loadavg | awk '{print $1}'`
loadLevel=$( printf "%0.f" $loadLevel )

# if the load level is greater than you want, start the script process. Otherwise, exit 0

#如果负载水平大于所需水平,请启动脚本过程。 否则,退出0

if [ $loadLevel -gt $levelToCheck ]; then echo "" > $tmpfile echo "**************************************" >>$tmpfile echo "Date: $dt " >>$tmpfile echo "Check System Load & Processes " >>$tmpfile echo "**************************************" >>$tmpfile

如果[$ loadLevel -gt $ levelToCheck]; 然后echo“”> $ tmpfile echo“ **************************************” >> $ tmpfile echo“ Date:$ dt” >> $ tmpfile echo“ Check System Load&Processes” >> $ tmpfile echo“ *********************** ***************“ >> $ tmpfile

And continue through the checks, writing the results to the temporary file. Add or delete items from here where applicable to your situation:

并继续进行检查,将结果写入临时文件。 在此处根据您的情况添加或删除项目:

# Get more variables from system:
httpdProcesses=`ps -def | grep httpd | grep -v grep | wc -l`

# Get more variables from system:
httpdProcesses=`ps -def | grep httpd | grep -v grep | wc -l`

# Show current load level: echo "Load Level Is: $loadLevel" >>$tmpfile echo "*************************************************" >>$tmpfile

#显示当前负载水平:echo“ Load Level Is:$ loadLevel” >> $ tmpfile echo“ ********************************* ********************“ >> $ tmpfile

# Show number of httpd processes now running (not including children): echo "Number of httpd processes now: $httpdProcesses" >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示正在运行的httpd进程数(不包括子进程):echo“现在httpd进程数:$ httpdProcesses” >> $ tmpfile echo“ ******************* **********************************“ >> $ tmpfile echo”“ >> $ tmpfile

# Show process list: echo "Processes now running:" >>$tmpfile ps f -ef >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示进程列表:echo“正在运行的进程:” >> $ tmpfile ps f -ef >> $ tmpfile echo“ ************************ *************************“” >> $ tmpfile echo“” >> $ tmpfile

# Show current MySQL info: echo "Results from mytop:" >>$tmpfile /usr/bin/mytop -u $dbusr -p $dbpw -b -d $db >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示当前MySQL信息:echo“ mytop的结果:” >> $ tmpfile / usr / bin / mytop -u $ dbusr -p $ dbpw -b -d $ db >> $ tmpfile echo“ ******* **********************************************“ >> $ tmpfile echo”“ >> $ tmpfile

Notice with the top command, we are writing to two temp files. One is for the much smaller message to cell phone. If you don’t want the urgency of cell phone alerts at three in the morning, you can take this out (and take out the second mailing routine later in the script).

注意,使用top命令,我们正在写入两个临时文件。 一种是发送给手机的小得多的消息。 如果您不希望在凌晨三点收到手机警报,可以将其删除(并在脚本的后面删除第二个邮寄例程)。


# Show current top:
echo "top now shows:" >>$tmpfile
echo "top now shows:" >>$topfile
/usr/bin/top -b -n1 >>$tmpfile
/usr/bin/top -b -n1 >>$topfile
echo "*************************************************" >>$tmpfile
echo "" >>$tmpfile


# Show current top:
echo "top now shows:" >>$tmpfile
echo "top now shows:" >>$topfile
/usr/bin/top -b -n1 >>$tmpfile
/usr/bin/top -b -n1 >>$topfile
echo "*************************************************" >>$tmpfile
echo "" >>$tmpfile

More checks:

更多检查:


# Show current connections:
echo "netstat now shows:" >>$tmpfile
/bin/netstat -p >>$tmpfile
echo "*************************************************" >>$tmpfile
echo "" >>$tmpfile


# Show current connections:
echo "netstat now shows:" >>$tmpfile
/bin/netstat -p >>$tmpfile
echo "*************************************************" >>$tmpfile
echo "" >>$tmpfile

# Check disk space echo "disk space:" >>$tmpfile /bin/df -k >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#检查磁盘空间echo“ disk space:” >> $ tmpfile / bin / df -k >> $ tmpfile echo“ ************************ *************************“” >> $ tmpfile echo“” >> $ tmpfile

Then write the temporary file contents to a more permanent log file and email the results to the appropriate parties. The second mailing is the pared down results consisting simply of the standard out of `top`:

然后将临时文件的内容写到一个更永久的日志文件中,并将结果通过电子邮件发送给相应的参与者。 第二封邮件是精简后的结果,仅包含`top`中的标准:

# Send results to log file:
/bin/cat $tmpfile >>$logfile

# Send results to log file:
/bin/cat $tmpfile >>$logfile

# And email results to sysadmin: /usr/bin/mutt -s "$machine has a high load level! - $dt" -a $mysqlLog -a $msgLog $mailstop >$logfile

#然后将结果通过电子邮件发送给sysadmin:/ usr / bin / mutt -s“ $ machine的负载很高!-$ dt” -a $ mysqlLog -a $ msgLog $ mailstop> $ logfile

And then some housekeeping and exit:

然后做一些客房整理并退出:

# And then remove the temp file:
rm $tmpfile
rm $topfile
fi

# And then remove the temp file:
rm $tmpfile
rm $topfile
fi

# exit 0

#退出0

Hopefully this helps someone out there. Fully assembled script is:

希望这可以帮助某人。 完全组装的脚本是:

#!/bin/bash
#
# Script to check system load average levels to try to determine what processes are
# taking it overly high...
#
# set environment
dt=`date +%d%b%Y-%X`
# Obviously, change the following directories to where your log files actually are kept
tmpfile="/tmp/checkSystemLoad.tmp"
logfile="/tmp/checkSystemLoad.log"
msgLog="/var/log/messages"
mysqlLog="/var/log/mysqld.log"
# the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report)
mailstop="sysadmin@mydomain.com"
mailstop1="15555555555@mycellphone.com"
machine=`hostname`
# The following three are for mytop use - use a db user that has decent rights
dbusr="username"
dbpw="password"
db="yourdatabasename"
# The following is the load level to check on - 10 is really high, so you might want to lower it.
levelToCheck=10
# Set variables from system:
loadLevel=`cat /proc/loadavg | awk '{print $1}'`
loadLevel=$( printf "%0.f" $loadLevel )

#!/bin/bash
#
# Script to check system load average levels to try to determine what processes are
# taking it overly high...
#
# set environment
dt=`date +%d%b%Y-%X`
# Obviously, change the following directories to where your log files actually are kept
tmpfile="/tmp/checkSystemLoad.tmp"
logfile="/tmp/checkSystemLoad.log"
msgLog="/var/log/messages"
mysqlLog="/var/log/mysqld.log"
# the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report)
mailstop="sysadmin@mydomain.com"
mailstop1="15555555555@mycellphone.com"
machine=`hostname`
# The following three are for mytop use - use a db user that has decent rights
dbusr="username"
dbpw="password"
db="yourdatabasename"
# The following is the load level to check on - 10 is really high, so you might want to lower it.
levelToCheck=10
# Set variables from system:
loadLevel=`cat /proc/loadavg | awk '{print $1}'`
loadLevel=$( printf "%0.f" $loadLevel )

# if the load level is greater than you want, start the script process. Otherwise, exit 0

#如果负载水平大于所需水平,请启动脚本过程。 否则,退出0

if [ $loadLevel -gt $levelToCheck ]; then echo "" > $tmpfile echo "**************************************" >>$tmpfile echo "Date: $dt " >>$tmpfile echo "Check System Load & Processes " >>$tmpfile echo "**************************************" >>$tmpfile

如果[$ loadLevel -gt $ levelToCheck]; 然后echo“”> $ tmpfile echo“ **************************************” >> $ tmpfile echo“ Date:$ dt” >> $ tmpfile echo“ Check System Load&Processes” >> $ tmpfile echo“ *********************** ***************“ >> $ tmpfile

# Get more variables from system: httpdProcesses=`ps -def | grep httpd | grep -v grep | wc -l`

#从系统获取更多变量:httpdProcesses =`ps -def | grep httpd | grep -v grep | wc -l`

# Show current load level: echo "Load Level Is: $loadLevel" >>$tmpfile echo "*************************************************" >>$tmpfile

#显示当前负载水平:echo“ Load Level Is:$ loadLevel” >> $ tmpfile echo“ ********************************* ********************“ >> $ tmpfile

# Show number of httpd processes now running (not including children): echo "Number of httpd processes now: $httpdProcesses" >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示正在运行的httpd进程数(不包括子进程):echo“现在httpd进程数:$ httpdProcesses” >> $ tmpfile echo“ ******************* **********************************“ >> $ tmpfile echo”“ >> $ tmpfile

# Show process list: echo "Processes now running:" >>$tmpfile ps f -ef >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示进程列表:echo“正在运行的进程:” >> $ tmpfile ps f -ef >> $ tmpfile echo“ ************************ *************************“” >> $ tmpfile echo“” >> $ tmpfile

# Show current MySQL info: echo "Results from mytop:" >>$tmpfile /usr/bin/mytop -u $dbusr -p $dbpw -b -d $db >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示当前MySQL信息:echo“ mytop的结果:” >> $ tmpfile / usr / bin / mytop -u $ dbusr -p $ dbpw -b -d $ db >> $ tmpfile echo“ ******* **********************************************“ >> $ tmpfile echo”“ >> $ tmpfile

# Show current top: echo "top now shows:" >>$tmpfile echo "top now shows:" >>$topfile /usr/bin/top -b -n1 >>$tmpfile /usr/bin/top -b -n1 >>$topfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示当前顶部:echo“顶部现在显示:” >> $ tmpfile echo“顶部现在显示:” >> $ topfile / usr / bin / top -b -n1 >> $ tmpfile / usr / bin / top -b- n1 >> $ topfile echo“ *********************************************** ******“ >> $ tmpfile echo”“ >> $ tmpfile

# Show current connections: echo "netstat now shows:" >>$tmpfile /bin/netstat -p >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#显示当前连接:echo“ netstat现在显示:” >> $ tmpfile / bin / netstat -p >> $ tmpfile echo“ ********************** *******************************“” >> $ tmpfile echo“” >> $ tmpfile

# Check disk space echo "disk space:" >>$tmpfile /bin/df -k >>$tmpfile echo "*************************************************" >>$tmpfile echo "" >>$tmpfile

#检查磁盘空间echo“ disk space:” >> $ tmpfile / bin / df -k >> $ tmpfile echo“ ************************ *************************“” >> $ tmpfile echo“” >> $ tmpfile

# Send results to log file: /bin/cat $tmpfile >>$logfile

#将结果发送到日志文件:/ bin / cat $ tmpfile >> $ logfile

# And email results to sysadmin: /usr/bin/mutt -s "$machine has a high load level! - $dt" -a $mysqlLog -a $msgLog $mailstop >$logfile

#然后将结果通过电子邮件发送给sysadmin:/ usr / bin / mutt -s“ $ machine的负载很高!-$ dt” -a $ mysqlLog -a $ msgLog $ mailstop> $ logfile

# And then remove the temp file: rm $tmpfile rm $topfile fi

#然后删除临时文件:rm $ tmpfile rm $ topfile fi

# exit 0

#退出0

翻译自: https://www.howtogeek.com/50934/diagnose-linux-server-load-problems-with-a-simple-script/

linux 重启服务器脚本

linux 重启服务器脚本_使用简单脚本诊断Linux服务器负载问题相关推荐

  1. linux系统下Qt应用程序重启,嵌入式Linux重启QT应用程序的简单办法(基于QT4.8 qws)...

    嵌入式Linux重启QT应用程序的简单办法(基于QT4.8 qws) 应用软件一般都有这样的业务需求: 当有新版本的APP,则程序就需要执行更新,更新完毕后(所谓的更新大多就是以覆盖的方式),不需要关 ...

  2. Linux上利用nginx搭建一个简单的rtmp视频流服务器(不涉及直播)

    文章目录 Linux上利用nginx搭建一个简单的rtmp视频流服务器(不涉及直播) 一.基础环境搭建 二.构建Nginx 下载nginx-rtmp-module 安装Nginx 编译nginx,代理 ...

  3. 我的世界服务器java启动脚本_我的世界 如何让服务器自动重启呢 自动重启脚本方法...

    今天为大家带来了<我的世界>自动重启的一个脚本,如何让服务器自动重启呢?那就来看看小编为大家带来的文章吧! 首先,你要有一个对应你服务器核心的插件,能让你的服务器实现定时关闭服务器. 说白 ...

  4. 我的世界服务器java启动脚本_我的世界定时关闭服务器 自动重启脚本

    我的世界定时关闭服务器 自动重启脚本.其实定时关闭服务器脚本还是有好处的,因为我们不可能永远都待在服务器的旁边,出问题的时候我们不可能第一时间去重启服务器查找问题的,所以老手腐竹们就需要一个定期重启的 ...

  5. java源码如何启动脚本_使用Shell脚本如何启动/停止Java的jar程序

    本文介绍如何使用Shell脚本来开启和停止jar程序的后台运行,以及如何实现out大文件的切分.另外,补充一些后台运行的小知识. 启动脚本:start_upload.sh #!/bin/sh nohu ...

  6. python代替shell脚本_自动化shell脚本except与python的pexpect模块

    expect脚本 expect是什么 expect是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预.说白了,expect就是一套用来实现自动交互功能的软件. 在实际工作中,我们运行命令. ...

  7. php邮件服务器搭建,如何快速简单的使用Linux搭建邮件服务器

    本篇文章给大家带来的内容是关于如何快速简单的使用Linux搭建邮件服务器,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 搭建邮件服务器,过程非常简单,只需几个步骤即可. 通常在Lin ...

  8. linux 查看进程端口_如何简单有效的查看windows进程使用了哪些端口

    概述 对于运维有时在排查网络问题时需要去查看进程使用的端口,下面整了一个bat脚本,主要利用netstat命令找出使用TCP协议通信的端口,并将结果分割:将第二个参数(IP加端口)传给%%i,第五个参 ...

  9. linux 环境变量文件_应急响应系列之Linux库文件劫持技术分析,有点硬核哟

    0×01 菜逼阶段 Linux库文件劫持这种案例在今年的9月份遇到过相应的案例,当时的情况是有台服务器不断向个可疑IP发包,尝试建立连接,后续使用杀软杀出木马,重启后该服务器还是不断的发包,使用net ...

最新文章

  1. 用spss做多组两两相关性分析_两独立样本T检验及如何利用SPSS实现其操作
  2. 林丹退役,用 Python 看看大家怎么说?
  3. (转)Activity的四种launchMode
  4. c语言产生1-6,C语言 1-6小结.ppt
  5. 常用开发环境搭建配置教程(OneStall)
  6. Python matplotlib绘制雷达图
  7. EF Ccore 主从配置 最简化
  8. 设计自有芯片将成为新常态?
  9. acrobat PDF删除部分_PDF原来可以这么玩
  10. Stanford University courses of computer science department(斯坦福计算机系课程设置)
  11. 前端项目总结:客运互联网售票平台
  12. 移民 萨大 计算机本科 移民家园,移民家园
  13. Android基于mAppWidget实现手绘地图(十一)–移动地图到某个坐标
  14. 渗透信息收集 子域名查询
  15. 算法时间复杂度大小排序
  16. 用 Python 实现马丁格尔交易策略(附代码)
  17. 网络知识:46张图带你了解网络传输、WIFI、以太网协议和网络寻址
  18. 常见外挂分类及原理概述
  19. 如何使流水号条码不重复打印
  20. 金笛短信猫应用消防调度指挥系统

热门文章

  1. ios开发之NSUserDefaults
  2. 计算机组播相关服务,Windows Server 2008 R2 之十八WDS(部署服务)之二
  3. xp系统使用u盘“提示请将磁盘插入驱动器”的操作流程--win10专业版
  4. Linux 文件处理3剑客之 awk
  5. Ubuntu单系统安装
  6. NVIDIA Jetson TX1介绍(二)
  7. 如何快速将json文件转换为对应的ts类型
  8. java如何调用本地扬声器
  9. linux 判断u盘 硬盘坏道,在 Linux 上检测硬盘上的坏道和坏块
  10. linux 只删除空文件夹,Linux如何删除空文件夹-linux删除文件夹