服务器常用脚本
#
参考资料 https://github.com/fengyuhetao/shell
常用定时器
#
- 自动休眠
- 清楚内存
- 更新容器内的git项目
- 定时器日志需要安装邮箱 apt install postfix -y (选择5. Local only)日志在/var/mail/用户下
*/10 * * * * echo 3 > /proc/sys/vm/drop_caches
0 */2 * * * docker exec zian updategit
powersave.sh
#
- 两小时自动关机
- debian13需安装
sudo su root + apt install -y wtmpdb
cat > /bin/powersave.sh << 'EOF'
#!/bin/bash
seconds=7200
uptime=$(($(date +%s) - $(stat -c %Y /proc/1)))
if [ $uptime -lt $seconds ]; then
echo "less than 2 hours ago"
exit 0
fi
since_time=$(date -d "$seconds seconds ago" '+%Y-%m-%d %H:%M:%S')
recent_ssh_login=$(last -s "$since_time" | grep "pts")
if [ -z "$recent_ssh_login" ]; then
echo "shutdown time:"$(date "+%Y-%m-%d %H:%M:%S")
/sbin/poweroff
fi
EOF
chmod +x /bin/powersave.sh
(crontab -l 2>/dev/null; echo "*/10 * * * * /bin/powersave.sh") | crontab -
updategit
#
cat > /bin/updategit << 'EOF'
#!/bin/bash
cd /var/www/
rm master*
command -v unzip >/dev/null 2>&1 || apt install -y unzip
wget https://github.com/zi-an/zi-an.github.io/archive/refs/heads/master.zip
unzip -o master.zip
EOF
kaibo.sh
#
cat > /bin/kaibo.sh << 'EOF'
#!/bin/bash
command -v ffmpeg >/dev/null 2>&1 || apt install -y ffmpeg
nohup ffmpeg -re -stream_loop -1 -i badapple.mp4 -c: copy -f flv "$1" &
EOF
chmod +x /bin/kaibo.sh
alias
#
- awk可能需要mawk跟gawk一起安装
- shfm格式化命令
cat > /etc/profile.d/mybash.sh << 'EOF'
export LANG='zh_CN.UTF-8'
alias httpproxy='export https_proxy=http://yun.mm:20172'
ff() {
find -iname "*$1*" ! -name ".*" 2>/dev/null
}
if [ "$(uname -s)" = "Linux" ]; then
if [ -z $SSH_TTY ]; then LANG=; fi
alias xunlei="docker start xunlei"
alias ollama='docker exec -it ollama sh'
alias qwen3='docker exec -it ollama ollama run qwen3:0.6b --think=false'
alias zian="docker exec -it zian bash"
alias test222='docker exec -it test222 bash'
alias transmission="docker exec -it transmission bash"
alias mailroot='sudo tail -n100 /var/mail/root | nl -ba'
alias maildocker='tail -n100 /var/mail/docker | nl -ba'
alias cpumen='ps -eo pid,pcpu,rss:8,cmd --sort=-rss --width 80| head -n 20'
alias dps="docker ps -a --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}'"
alias v2rayalog="docker logs -fn20 v2raya | awk '{print substr(\$2,1,8), \$5, \$7, \$8}'"
else
alias 5.mm='ssh docker@5.mm'
alias 221.mm='ssh root@221.mm'
alias 222.mm='ssh root@222.mm'
alias yun.mm='ssh docker@yun.mm'
alias s1='ssh root@yun.mm -p1111'
alias s2='ssh root@yun.mm -p2222'
alias java='/d/tools/jdk21/bin/java'
alias redis='redis-cli.exe -h yun.mm -a zian'
alias mysql='mysql-cli.exe -h yun.mm -u zian -p'zian' zian'
alias postgresql='/d/tools/shell/postgresql/psql.exe -h yun.mm -U postgres' # setx PGPASSWORD zian
gobuildlinux() {
mkdir -p /z/.go
export GOCACHE=/z/.go TMPDIR=/z/.go
export CGO_ENABLED=0 GOOS=linux GOARCH=amd64
go build -x -trimpath -ldflags "-s -w -X main.TGBot=$TGBot" -o /z/$1 $2
#gobuildlinux monitors ./cmd/monitors/ 编译目录需要从.开始
}
fi
EOF
更新mtproto
#
apt update
apt install -y libnginx-mod-stream nmap
rm /etc/nginx/modules-enabled/*
sed -i '6d' /etc/nginx/nginx.conf
sed -i '6i stream {include /etc/nginx/modules-enabled/*.conf;}' /etc/nginx/nginx.conf
sed -i '6i load_module modules/ngx_stream_module.so;' /etc/nginx/nginx.conf
cat > /etc/nginx/modules-enabled/.conf << 'EOF'
upstream mtprotoPort {
}
server {
listen Port;
proxy_pass mtprotoPort;
}
EOF
cat > /bin/mtproto.sh << 'EOF'
#!/bin/bash
TG_URL=${1:-"https://t.me/s/qiuyue2"}
TG_PORT=${2:-"10060"}
Port=${3:-"91"}
for count in {1..5}; do
echo $count
ip=$(curl --retry 9 -s $TG_URL --proxy socks5h://yun.mm:20170)
if [ ${#ip} -gt 100 ]; then
ip=$(echo "$ip" | grep -oP 'server=\K[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | tail -n1)
ip=$(nmap --open -p $TG_PORT "${ip%.*}".1-255 | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
echo "$ip" | uniq | sort | sed "s/^/server /;s/$/:$TG_PORT;/" >/mtproto.txt
sed '1r /mtproto.txt' /etc/nginx/modules-enabled/.conf >/etc/nginx/modules-enabled/$Port.conf
sed -i "s|Port|$Port|g" /etc/nginx/modules-enabled/$Port.conf
cat /etc/nginx/modules-enabled/$Port.conf
nginx -s reload
break
fi
done
EOF
chmod +x /bin/mtproto.sh