服务器常用脚本
#
参考资料 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 bbb.mp4 -c: copy -f flv "$1" &
EOF
alias
#
cat > /etc/profile.d/mybash.sh << 'EOF'
#if [ -z $SSH_TTY ];then LANG=;fi
alias mailroot='sudo tail -n100 /var/mail/root | nl -ba'
alias maildocker='tail -n100 /var/mail/docker | nl -ba'
alias httpproxy='export https_proxy=http://yun.mm:20172'
alias xunlei="docker start xunlei"
alias zian="docker exec -it zian bash"
alias test222='docker exec -it test222 bash'
alias transmission="docker exec -it transmission bash"
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}'"
ff() {
find -iname "*$1*" ! -name ".*"
}
EOF
更新mtproto
#
- 编辑配置文件
- 编写mtproto.sh,防止空返回加多一次curl
- 定时器 */10 * * * * docker exec -i zian mtproto.sh
- apt install -y nmap
apt update
apt install libnginx-mod-stream -y
cat > /etc/nginx/modules-enabled/.conf << 'EOF'
stream {
upstream mtproto {
}
server {
listen 81;
proxy_pass mtproto;
}
}
EOF
cat > /bin/mtproto.sh << 'EOF'
#!/bin/bash
port=47727
for count in {1..5}; do
echo $count
ip=$(curl --retry 9 -s https://t.me/s/ProxyMTProting --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 $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/\$/:$port;/" >/mtproto.txt
sed '2r /mtproto.txt' /etc/nginx/modules-enabled/.conf >/etc/nginx/modules-enabled/81.conf
cat /etc/nginx/modules-enabled/81.conf
nginx -s reload
break
fi
done
EOF
chmod +x /bin/mtproto.sh