Linux运维:shell脚本(3)
总结一下碰到过的shell脚本。
1、统计进程占用内存大小
写一个脚本计算一下linux系统所有进程占用内存大小的和。
#!/bin/bash
sum=0
for mem in `ps aux | awk '{print $6}' | grep -v 'RSS'`
do
sum=$[$sum + $mem]
done
echo "The total memory is $sum"2、批量创建用户
写一个脚本实现:
添加user_00-user-09总共10个用户,并且对这10个用户设置一个随机密码,密码要求10位包含大小写字母以及数字,最后将每个用户的密码记录到一个日志文件里。
#!/bin/bash
for i in `seq -w 0 09`
do
useradd user_$i
p=`mkpasswd -s 0 -l 10`
echo "user_$i $p" >> /tmp/user0_9.pass
echo $p |passwd --stdin user_$i
done3、URL检测脚本
编写shell脚本检测URL是否正常。
#!/bin/bash
. /etc/init.d/functions
function usage(){
echo $"usage:$0 url"
exit 1
}
function check_url(){
wget --spider -q -o /dev/null --tries=1 -T 5 $1
if [ $? -eq 0 ];then
action "$1 is yes." /bin/true
else
action "$1 is no." /bin/false
fi
}
function main(){
if [ $# -ne 1 ];then
usage
fi
check_url $1
}
main $*测试结果:
[root@moli_linux1 script]# sh check_url.sh www.baidu.com www.baidu.com is yes. [ 确定 ] [root@moli_linux1 script]# sh check_url.sh www.baiduxxx.com www.baiduxxx.com is no. [失败]
相关推荐
shipinsky 2020-09-23
touchfuture 2020-09-16
hongsheyoumo 2020-09-04
laisean 2020-11-11
zhangjie 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
CARBON 2020-10-20