linux常用的shell脚本
一、检查系统负载:检查系统的负载情况,并在超过阈值时发送警报。可使用 uptime
命令和条件语句来实现
#!/bin/bash
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')THRESHOLD=80THRESHOLD=80if (( $(echo "$CPU_USAGE > $THRESHOLD" | bc -l) )); then
echo"CPU usage is high: $CPU_USAGE%"
# Send alert email or trigger automated responsefi
二、备份文件:编写一个脚本以定期备份指定目录的文件。可以使用 cp
命令和 cron
作业调度程序来完成
#!/bin/bash
backup_dir="/path/to/backup"
source_dir="/path/to/source"
timestamp=$(date +%Y%m%d%H%M%S)
backup_file="backup_$timestamp.tar.gz"
tar czf "$backup_dir/$backup_file" "$source_dir"
三、清理日志文件:编写一个脚本以清理旧的日志文件,保留最近的一段时间内的文件。可以使用 find
命令和条件语句来实现
#!/bin/bash
log_dir="/path/to/logs"
days_to_keep=7
find "$log_dir" -type f -name "*.log" -mtime +$days_to_keep -delete
四、监控服务状态:编写一个脚本以监控关键服务的运行状态,并在服务异常时发送警报。可以使用 systemctl
命令和条件语句来实现
#!/bin/bash
service_name="nginx"
if ! systemctl is-active --quiet "$service_name"; then
echo "服务 $service_name 未运行" | mail -s "服务状态警报" [email protected]
fi
五、自动化部署:编写一个脚本以自动化部署应用程序或配置文件到多台服务器。可以使用 rsync
命令和循环结构来实现
#!/bin/bash
servers=("server1" "server2" "server3")
source_dir="/path/to/source"
destination_dir="/path/to/destination"
for server in "${servers[@]}"; do
rsync -avz "$source_dir" "$server:$destination_dir"
done
六、监控磁盘空间:编写一个脚本以监控系统磁盘空间使用情况,并在空间不足时发送警报。可以使用 df
命令和条件语句来实现
#!/bin/bash
threshold=90
df_output=$(df -h)
while read -r line; do
usage=$(echo "$line" | awk '{print $5}' | sed 's/%//')
if (( usage > threshold )); then
echo "磁盘空间不足: $line" | mail -s "磁盘空间警报" [email protected]
fi
done <<< "$df_output"
七、启动/停止服务:编写一个脚本以同时启动或停止多个服务可以使用 systemctl 命令和循环结构来实现
#!/bin/bash
services=("service1" "service2" "service3")
action="start" # 或者 "stop"
for service in "${services[@]}"; do
systemctl "$action" "$service"
done
八、批量修改服务器用户密码
# cat old_pass.txt
192.168.18.217 root 123456 22
192.168.18.218 root 123456 22
内容格式:IP User Password Port
SSH远程修改密码脚本:新密码随机生成
https://www.linuxprobe.com/books
#!/bin/bash
OLD_INFO=old_pass.txt
NEW_INFO=new_pass.txt
for IP in $(awk '/^[^#]/{print $1}' $OLD_INFO); do
USER=$(awk -v I=$IP 'I==$1{print $2}' $OLD_INFO)
PASS=$(awk -v I=$IP 'I==$1{print $3}' $OLD_INFO)
PORT=$(awk -v I=$IP 'I==$1{print $4}' $OLD_INFO)
NEW_PASS=$(mkpasswd -l 8) # 随机密码
echo "$IP $USER $NEW_PASS $PORT" >> $NEW_INFO
expect -c "
spawn ssh -p$PORT $USER@$IP
set timeout 2
expect {
\"(yes/no)\" {send \"yes\r\";exp_continue}
\"password:\" {send \"$PASS\r\";exp_continue}
\"$USER@*\" {send \"echo \'$NEW_PASS\' |passwd --stdin $USER\r exit\r\";exp_continue}
}"
done
生成新密码文件:
# cat new_pass.txt
192.168.18.217 root n8wX3mU% 22
192.168.18.218 root c87;ZnnL 22
Linux主机SSH连接信息:旧密码
九、数据处理脚本
#!/bin/bash
DATA_FILE="data.csv"
OUTPUT_FILE="processed_data.csv"
# Remove header and extract specific columns
tail -n +2 "$DATA_FILE" | cut -d ',' -f 1,3 > "$OUTPUT_FILE"
echo "Data processing completed. Processed data saved to $OUTPUT_FILE"
十、自动化测试脚本
#!/bin/bash
TEST_URL="http://example.com"
EXPECTED_RESPONSE="200 OK"
TEST_RESULT=$(curl -s -o /dev/null -w "%{http_code}" $TEST_URL)
if [ "$TEST_RESULT" = "$EXPECTED_RESPONSE" ]; then
echo "Test passed: HTTP status code is 200 OK"
else
echo "Test failed: HTTP status code is not 200 OK"
fi
十一、日常维护脚本
#!/bin/bash
# Check disk space usage
df -h
# Check system load
uptime
# Check running processes
ps aux
# Check system logs for errors
tail /var/log/syslog | grep "error"
# Check for available software updates
apt update
apt list --upgradable
十二、性能优化脚本
#!/bin/bash
# Check CPU usage
top -b -n 1 | grep "Cpu(s)"
# Check memory usage
free -h
# Check disk I/O
iostat
# Check network usage
iftop
# Check for high load processes
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu
标签:脚本,bin,shell,echo,dir,linux,Check,bash From: https://www.cnblogs.com/hkgan/p/18143154