Skip to content
Advertisement

Bash poweroff script hangs system

My intention is to cycle through my list of ips and poweroff if my ping succeeds first. However the systems seems to hang. After running this script I can’t ping the systems anymore and they aren’t powered off. If I run ssh 192.168.1.ip "sudo poweroff" through terminal I dont encounter this issue. Any advice?

for ((ip=40, cnt=0; ip<=max; ip++, cnt++))
do
    if ping -c 1 192.168.1.$ip &> /dev/null
    then
        printf "n${array[$cnt]}: Ping Successful"
        ssh 192.168.1.$ip "sudo poweroff"
        printf "n${array[$cnt]}: Power Down Executedn"
        sleep 1
    else
        printf "n${array[$cnt]}: Ping Failedn"    
    fi
done

After running a single ssh 192.168.1.40 "sudo poweroff" the system is properly powered off. When running my script, I am unable to ping the systems however I can visually see the fans and leds are still on. I think I should use a KVM to take a closer look since ssh doesn’t allow allow connection after this script is run. Still at first glance I dont understand how running ssh 192.168.1.40 "sudo poweroff" and running it through my script really makes a difference. Anyways I’ll try to add more information tomorrow.

Advertisement

Answer

ssh 192.168.1.$ip “(sleep 5; sudo poweroff)&” to put the process in the background on the remote host and sleep for 5 seconds before powering off to give time for the script to complete and exit the remote host before it goes down… – David C. Rankin

This Resolved my Issue.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement