Linux 常用脚本

以下是几个 Linux 常用脚本示例:

  1. 自动备份脚本

这个脚本用于定期备份指定目录中的文件,并将备份文件保存到另一个目录中。

#!/bin/bash

backup_dir="/home/user/backup/"
source_dir="/home/user/important_data/"
date=$(date +'%Y-%m-%d')
backup_file="backup-$date.tar.gz"

tar -czf $backup_dir$backup_file $source_dir

echo "Backup completed successfully"
  1. 自动清理临时文件脚本

这个脚本用于清理指定目录下的所有文件,以及指定天数前的所有文件。

#!/bin/bash

temp_dir="/tmp/"
max_age_days=7

find $temp_dir -type f -mtime +$max_age_days -exec rm {} \;
rm -f $temp_dir*
echo "Temp files cleaned up successfully"
  1. 网站定时截图脚本

这个脚本使用 PhantomJS 和 CasperJS 来自动化截图网页并保存截图。

#!/bin/bash

site_url="http://www.example.com"
output_dir="/home/user/screenshots/"

casperjs --ssl-protocol=any /path/to/screenshot.js "$site_url" "$output_dir"

其中 screenshot.js 是执行截图的脚本,可以定义网页的大小、图片格式等选项。

  1. 目录同步脚本

这个脚本用于定期将本地指定目录的文件同步到远程服务器。

#!/bin/bash

local_dir="/home/user/data/"
remote_dir="/mnt/remote_data/"
remote_server="user@remote.server.com"

rsync -avz $local_dir $remote_server:$remote_dir

echo "Data synced successfully"
  1. 端口扫描脚本

这个脚本用于扫描指定的 IP 地址和端口,检查它们是否可用。

#!/bin/bash

host="www.example.com"
port=80

nc -zv $host $port > /dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "The port is open"
else
    echo "The port is closed"
fi
  1. 自动更新软件脚本

这个脚本用于定期自动更新系统中的所有软件包。

#!/bin/bash

sudo apt-get update -y
sudo apt-get upgrade -y

echo "System updated successfully"
  1. 监控系统资源脚本

这个脚本用于监控系统的 CPU、内存和磁盘使用情况,并在达到某个阈值时发送警报邮件。

#!/bin/bash

cpu_threshold=80
mem_threshold=80
disk_threshold=80
email_recipient="user@example.com"

get_cpu_usage() {
    top -bn1 | grep load | awk '{printf "%.2f%%\n", $(NF-2)}'
}

get_mem_usage() {
    free | grep Mem | awk '{printf "%.2f%%\n", $3/$2 * 100.0}'
}

get_disk_usage() {
    df -h | awk '$NF=="/"{printf "%s\n", $5}'
}

if [ $(get_cpu_usage | cut -d'.' -f1) -gt $cpu_threshold ]; then
    echo "CPU usage is above the threshold, sending email"
    echo "CPU usage is at $(get_cpu_usage)" | mailx -s "CPU Usage Alert" $email_recipient
fi

if [ $(get_mem_usage | cut -d'.' -f1) -gt $mem_threshold ]; then
    echo "Memory usage is above the threshold, sending email"
    echo "Memory usage is at $(get_mem_usage)" | mailx -s "Memory Usage Alert" $email_recipient
fi

if [ $(get_disk_usage | cut -d'%' -f1) -gt $disk_threshold ]; then
    echo "Disk usage is above the threshold, sending email"
    echo "Disk usage is at $(get_disk_usage)" | mailx -s "Disk Usage Alert" $email_recipient
fi
  1. 自动清理旧日志脚本

这个脚本用于定期清理指定目录下的旧日志文件。

#!/bin/bash

log_dir="/var/log/"
max_age_days=30

find $log_dir -type f -mtime +$max_age_days -exec rm {} \;
echo "Log files cleaned up successfully"
  1. 自动部署脚本

这个脚本用于自动部署应用程序及其依赖,例如 Django 应用程序。

#!/bin/bash

app_name="myapp"
app_dir="/opt/myapp/"
venv_dir="/opt/myapp/env/"

git pull origin master

source $venv_dir/bin/activate
pip install -r requirements.txt
python manage.py migrate
python manage.py collectstatic --noinput
sudo systemctl restart $app_name

echo "Application deployed successfully"
  1. 定时重启服务脚本

这个脚本用于定时重启指定的服务,以确保其稳定运行。

#!/bin/bash

service_name="my_service"
max_uptime_seconds=$((24*60*60))  # 1天

while true
do
    uptime_seconds=$(systemctl show -p ActiveEnterTimestamp $service_name | awk -F '=' '{print $2}')

    if [ $uptime_seconds -ge $max_uptime_seconds ]; then
        sudo systemctl restart $service_name
        echo "Service restarted successfully"
    fi

    sleep 3600  # 每小时检查一次
done

以上是几个常用的 Linux 脚本示例,它们可以帮助你自动化各种系统管理任务,提高工作效率和可靠性。

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hhb_618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值