Linux Shell 脚本—用户活动、查看进程、显示内存
【摘要】 您可能会发现以下三个 Linux shell 脚本很有帮助。
根据 %CPU 或内存使用情况显示进程。
显示哪个用户使用 CPU 最多。
显示系统的内存信息——总数、已用和空闲。
您可能会发现以下三个 Linux shell 脚本很有帮助。
- 根据 %CPU 或内存使用情况显示进程。
- 显示哪个用户使用 CPU 最多。
- 显示系统的内存信息——总数、已用和空闲。
1.根据%CPU和内存使用率列出进程
此脚本根据 %CPU 和内存使用率列出进程,不带参数(默认情况下),如果指定参数(cpu 或 mem),它将根据 CPU 使用率或内存使用率列出进程。
$ cat processes.sh
#! /bin/bash
#List processes based on %cpu and memory usage
echo "Start Time" `date`
# By default, it display the list of processes based on the cpu and memory usage #
if [ $# -eq 0 ]
then
echo "List of processes based on the %cpu Usage"
ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu # sorted based on %cpu
echo "List of processes based on the memory Usage"
ps -e -orss=,args= | sort -b -k1,1n # sorted bases rss value
# If arguements are given (mem/cpu)
else
case "$1" in
mem)
echo "List of processes based on the memory Usage"
ps -e -orss=,args= | sort -b -k1,1n
;;
cpu)
echo "List of processes based on the %cpu Usage"
ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu
;;
*)
echo "Invalid Argument Given \n"
echo "Usage : $0 mem/cpu"
exit 1
esac
fi
echo "End Time" `date`
exit 0
您可以执行上述脚本,如下所示。
$ processes.sh
$ processes.sh mem
$ processes.sh cpu
2.显示登录用户和使用高CPU百分比的用户
这个脚本显示了一些关于当前登录用户和他们在做什么的信息。
$ cat loggedin.sh
#! /bin/bash
w > /tmp/a
echo "Total number of unique users logged in currently"
cat /tmp/a| sed '1,2d' | awk '{print $1}' | uniq | wc -l
echo ""
echo "List of unique users logged in currently"
cat /tmp/a | sed '1,2d'| awk '{print $1}' | uniq
echo ""
echo "The user who is using high %cpu"
cat /tmp/a | sed '1,2d' | awk '$7 > maxuid { maxuid=$7; maxline=$0 }; END { print maxuid, maxline }'
echo ""
echo "List of users logged in and what they are doing"
cat /tmp/a
$ ./loggedin.sh
Total number of unique users logged in currently
4
List of unique users logged in currently
john
david
raj
reshma
The user who is using high %cpu
0.99s reshma pts/5 192.168.2.1 15:26 3:01 1.02s 0.99s custom-download.sh
List of users logged in and what they are doing
15:53:55 up 230 days, 2:38, 7 users, load average: 0.19, 0.26, 0.24
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
john pts/1 192.168.2.9 14:25 1:28m 0.03s 0.03s -bash
john pts/2 192.168.2.9 14:41 1:11m 0.03s 0.03s -bash
raj pts/0 192.168.2.6 15:07 9:08 0.11s 0.02s -bash
raj pts/3 192.168.2.6 15:19 29:29 0.02s 0.02s -bash
john pts/4 192.168.2.91 15:25 13:47 0.22s 0.20s vim error_log
reshma pts/5 192.168.2.1 15:26 3:01 1.02s 0.99s custom-download.sh
3. 显示总内存、已用内存和可用内存
以下脚本显示总内存空间、已用内存空间和可用内存空间。
$ cat mem.sh
#! /bin/bash
# Total memory space details
echo "Memory Space Details"
free -t -m | grep "Total" | awk '{ print "Total Memory space : "$2 " MB";
print "Used Memory Space : "$3" MB";
print "Free Memory : "$4" MB";
}'
echo "Swap memory Details"
free -t -m | grep "Swap" | awk '{ print "Total Swap space : "$2 " MB";
print "Used Swap Space : "$3" MB";
print "Free Swap : "$4" MB";
}'
$ ./mem.sh
Memory Space Details
Total Memory space : 4364 MB
Used Memory Space : 451 MB
Free Memory : 3912 MB
Swap memory Details
Total Swap space : 2421 MB
Used Swap Space : 0 MB
Free Swap : 2421 MB
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)