简体   繁体   中英

Python script to reboot server N times

I'm trying to stress test several servers that I can ssh into. I'm trying to write a python script that causes a reboot loop for N times. I call

os.system('reboot') 

But, I'm not sure how to have the script continue execution once the server has finished booting to continue execution. The servers do run various distros of Linux. Any help would be great.

You mentioned that the solution doesn't have to be in Python, so you can just use a Bash script for this (given that you can ping the server):

#!/usr/bin/env bash
COUNTER=$1
SERVER=$2
COMMAND="sudo reboot"
SLEEP_DURATION=60

echo "Working on $SERVER $COUNTER times"

while (( $COUNTER > 0 )); do
    ping -c 1 -t 5 $SERVER;
    _ping_r=$?
    if (( $_ping_r < 1 )); then
        echo "Rebooting $SERVER"
        ssh $SERVER $COMMAND;
        let COUNTER=COUNTER-1
    else
        echo "Couldn't ping $SERVER.  Taking a quick nap and trying again."
        sleep 5
    fi
    sleep $SLEEP_DURATION;
done

echo "Done working on $SERVER"

Save it in something like command_runner.sh and simply call it via ./command_runner.sh 2 server.example.org on a workstation that can SSH and run reboot on the server.

您可以使用Fabric将 ssh 并行连接到多个服务器并在那里执行各种命令(即使是那些需要重新启动的命令——在fabfile.py此类命令后,您可能需要在fabfile.py明确断开所有服务器的连接)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM