monk
monk

Reputation: 2115

How to keep linux machine shutdown for fixed duration

I am trying to simulate a condition for a Test case were linux server is down for 10 minutes. I am aware how to shutdown the machine but it will auto restart if I issue reboot command. Currently I am using following way to keep target down for 600 sec. but it is not robust as sometimes it lead target to partially come up before another reboot. leading to incorrect result.

target="active001"
secs=600  ;SECONDS=0 ; 
while (( SECONDS < secs )); 
do
  ssh ${target}  reboot
  sleep 1;
done

Upvotes: 0

Views: 117

Answers (1)

arif yan
arif yan

Reputation: 11

#!/bin/bash
# ...<3600
STATUS="active001"
TARGET_SECOND="600"

TARGET_TIME=$(($(date +%M)+$TARGET_SECOND))
TARGET_TIME_UP=$(($(date +%s)+$TARGET_SECOND))
START_TIME=$(date +%s)

printf "\033[01;35mStarting Schedhule Reboot if need... \033[01;32mInterval ($(date -d @$TARGET_TIME +%M) Minute)\033[01;00m\n"    
while true; do
    TIME_NOW=$(date +%s)    
    DELTATIME=$(($TARGET_TIME_UP - $TIME_NOW))    
    TIMELAPSE=$(date -ud "@$DELTATIME" +'%H:%M:%S')    
    if [ $TIME_NOW = $TARGET_TIME_UP ];then 
        #printf "\nRunning script command...\n"
        ssh ${STATUS} reboot
        #end loop or other option
        break
    fi
    printf "\r$(date -d @$TIME_NOW +%H:%M:%S) \033[01;32mReboot Execute on: \033[01;32m$(date -d @$TARGET_TIME_UP +%H:%M:%S)\033[00;00m [ ELAPSED : $TIMELAPSE ] "
    sleep 1
done

Upvotes: 1

Related Questions