CPU脚本并远程部署

CPU脚本

shell 复制代码
#!/bin/bash
# filename: cputest.sh

endless_loop()
{
	local marker="cputest_worker_$$"
	# 使用 bash -c 执行,并在命令中包含唯一标识
	/bin/bash -c "i=0; while true; do ((i+=100)); i=100; done # $marker" &
}

if [ $# != 1 ]; then
	echo "USAGE: $0 <CPUs>"
	exit 1
fi

declare -a pid_array

for i in $(seq "$1"); do
	endless_loop
	pid_array+=($!)
done

echo "Started ${#pid_array[@]} CPU stress processes:"
for pid in "${pid_array[@]}"; do
	echo "  PID: $pid (kill with: kill $pid)"
done

echo
echo "To find them later, run:  ps -ef | grep 'cputest_worker_'"

远程部署到主机

shell 复制代码
#!/bin/bash
# filename: deploy.sh

# 配置
SCRIPT_LOCAL="cputest.sh"         # 本地脚本路径
REMOTE_USER="root"                # 远程用户名,如 ubuntu、root 等
REMOTE_DIR="/data/shell/res"      # 远程存放目录

# 遍历 IP 段 192.168.31.1 到 192.168.31.10
for i in {1..10}; do
    IP="10.78.71.$i"
    echo ">>> Processing $IP ..."

    # 创建没记录
    ssh -q "${REMOTE_USER}@${IP}" "mkdir -p ${REMOTE_DIR}"

    # 拷贝脚本到远程主机
    scp -q "$SCRIPT_LOCAL" "${REMOTE_USER}@${IP}:${REMOTE_DIR}/${SCRIPT_LOCAL}" || {
        echo "❌ Failed to copy to $IP"
        continue
    }

    # 远程执行
    ssh -q $REMOTE_USER@$IP <<EOF 2>&1 | grep -v -E "Authorized|Web console" | grep -v '^$'

cd $REMOTE_DIR
# 关闭之前的进程
ps -ef | grep cputest_worker | grep -v grep | awk '{print \$2}' | xargs -r kill -9
# 设置权限
chmod +x ./$SCRIPT_LOCAL
# 后台执行
nohup ./$SCRIPT_LOCAL 1 >> /dev/null 2>&1 &

EOF

    echo "✅ Successfully deployed and executed on $IP"
done
相关推荐
EverydayJoy^v^7 天前
Linux Shell 高级编程(3)——awk
linux·运维·shell
dingdingfish10 天前
Bash学习 - 第10章:Installing Bash
bash·make·shell·install·configure·5.3
dingdingfish11 天前
Bash学习 - 第8章:Command Line Editing,第6-8节:Programmable Completion
bash·shell·completion·complete·compgen·compopt
白云偷星子12 天前
RHCSA笔记3
shell
dingdingfish12 天前
Bash学习 - 第7章:Job Control
bash·shell·wait·job
dingdingfish13 天前
Bash学习 - 第8章:Command Line Editing,第1-2节:Intro & Readline Interaction
bash·shell·readline
only_Klein13 天前
Shell 三剑客
shell·sed·grep·awk
dingdingfish14 天前
Bash学习 - 第6章:Bash Features,第12节:Shell Compatibility Mode
bash·shell·compat·compatibility
alanesnape14 天前
一个支持在线deBug的编辑器/调试器功能详解
shell·在线编译器·在线debug
dingdingfish15 天前
Bash学习 - 第6章:Bash Features,第10节:The Restricted Shell
bash·shell·rbash·restrict