#!/bin/bash
useradd "$1"
echo "$2" |passwd --stdin "$1"
.2. 每周 5 使用 tar 命令备份/var/log 下的所有日志文件
编写脚本
#!/bin/bash
tar -zcf log-`date +%F`.tar.gz /var/log
chmod +x tar.sh
设置定时任务
crontab -e
3. 监控本机内存和硬盘剩余空间,剩余内存小于 500M、根分区剩余空间小于 1000M
时,发送报警邮件给 root 管理员
#!/bin/bash
gen_size=$(df / |awk '/\//{print $4}')
mem_size=$(free |awk '/Mem/{print $4}')
while :
do
if [ $gen_size -le 512000 -a $mem_size -le 1024000 ]
then
mail -s 'mail test' [email protected] < con.txt
fi
done
#!/bin/bash
num=$[RANDOM%100+1]
while :
do
read -p "计算机生成了一个 1-100 的随机数,你猜: " num
if [ $num -eq $num ];then
echo "恭喜,猜对了"
exit
elif [ $num -gt $num ];then
echo "Oops,猜大了"
else
echo "Oops,猜小了"
fi
done
#!/bin/bash
if [ $USER == "root" ]
then
yum -y install vsftpd
else
echo "您不是管理员,没有权限安装软件"
fi
#!/bin/bash
read -p "请输入一个整数:" num1
read -p "请输入一个整数:" num2
read -p "请输入一个整数:" num3
tmp=0
if [ $num1 -gt $num2 ];then
tmp=$num1
num1=$num2
num2=$tmp
fi
if [ $num1 -gt $num3 ];then
tmp=$num1
num1=$num3
num3=$tmp
fi
if [ $num2 -gt $num3 ];then
tmp=$num2
num2=$num3
num3=$tmp
fi
echo "排序后数据为:$num1,$num2,$num3"
#!/bin/bash
myping(){
ping -c2 -i0.3 -W1 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "$1 is up"
else
echo "$1 is down"
fi
}
for i in {1..24}
do
myping 10.0.0.$i &
done
#!/bin/bash
for i in "ls *.$1"
do
mv $i ${i%.*}.$2
done
9. 切割 Nginx 日志文件(防止单个文件过大,后期处理很困难)
vim nginx_log.sh
在这里插入代码片#!/bin/bash
logs_path="/usr/local/nginx/logs/"
mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
chmod +x nginx_log.sh
#!/bin/bash
a=`mysqladmin -uroot -p123 status|awk '{print $4}'`
if [ $a -gt 500 ]
then
mail -s 'mail test' [email protected] < con.txt
fi