Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Forked from justinshenk/idle-shutdown.sh
Created October 27, 2022 11:55
Show Gist options
  • Save ramnathv/0ce715a32c119be870a2d68f319858e3 to your computer and use it in GitHub Desktop.
Save ramnathv/0ce715a32c119be870a2d68f319858e3 to your computer and use it in GitHub Desktop.

Revisions

  1. @justinshenk justinshenk revised this gist Jul 3, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # instance-name --metadata-from-file startup-script=idle-shutdown.sh` and reboot
    # NOTE: requires `bc`, eg, sudo apt-get install bc
    # Modified from https://stackoverflow.com/questions/30556920/how-can-i-automatically-kill-idle-gce-instances-based-on-cpu-usage
    threshold=0.4
    threshold=0.1

    count=0
    wait_minutes=60
  2. @justinshenk justinshenk revised this gist Jul 3, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ wait_minutes=60
    while true
    do

    load=$(uptime | sed -e 's/.*load average: //g' | awk '{ print $3 }') # 1-minute average load
    load=$(uptime | sed -e 's/.*load average: //g' | awk '{ print $1 }') # 1-minute average load
    load="${load//,}" # remove trailing comma
    res=$(echo $load'<'$threshold | bc -l)
    if (( $res ))
  3. @justinshenk justinshenk revised this gist Jun 25, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,8 @@ wait_minutes=60
    while true
    do

    load=$(uptime | sed -e 's/.*load average: //g' | awk '{ print $1 }') # 1-minute average load
    load=$(uptime | sed -e 's/.*load average: //g' | awk '{ print $3 }') # 1-minute average load
    load="${load//,}" # remove trailing comma
    res=$(echo $load'<'$threshold | bc -l)
    if (( $res ))
    then
  4. @justinshenk justinshenk revised this gist Jun 24, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,6 @@ threshold=0.4

    count=0
    wait_minutes=60

    while true
    do

  5. @justinshenk justinshenk revised this gist Jun 24, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,8 @@
    threshold=0.4

    count=0
    wait_minutes=60

    while true
    do

    @@ -18,7 +20,7 @@ do
    fi
    echo "Idle minutes count = $count"

    if (( count>60 )) # number of minutes
    if (( count>wait_minutes ))
    then
    echo Shutting down
    # wait a little bit more before actually pulling the plug
  6. @justinshenk justinshenk revised this gist Jun 24, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # Add to instance metadata with `gcloud compute instances add-metadata \
    # instance-name --metadata-from-file startup-script=idle-shutdown.sh`
    # instance-name --metadata-from-file startup-script=idle-shutdown.sh` and reboot
    # NOTE: requires `bc`, eg, sudo apt-get install bc
    # Modified from https://stackoverflow.com/questions/30556920/how-can-i-automatically-kill-idle-gce-instances-based-on-cpu-usage
    threshold=0.4
  7. @justinshenk justinshenk revised this gist Jun 24, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # Add to instance metadata with gcloud compute instances add-metadata \
    # instance-name --metadata-from-file startup-script=idle-shutdown.sh
    # Add to instance metadata with `gcloud compute instances add-metadata \
    # instance-name --metadata-from-file startup-script=idle-shutdown.sh`
    # NOTE: requires `bc`, eg, sudo apt-get install bc
    # Modified from https://stackoverflow.com/questions/30556920/how-can-i-automatically-kill-idle-gce-instances-based-on-cpu-usage
    threshold=0.4
  8. @justinshenk justinshenk revised this gist Jun 24, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/bin/bash
    # Add to instance metadata with gcloud compute instances add-metadata \
    # instance-name --metadata-from-file startup-script=idle-shutdown.sh
    # NOTE: requires `bc`, eg, sudo apt-get install bc
    # Modified from https://stackoverflow.com/questions/30556920/how-can-i-automatically-kill-idle-gce-instances-based-on-cpu-usage
    threshold=0.4

  9. @justinshenk justinshenk renamed this gist Jun 24, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion auto-shutdown.sh → idle-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash
    # Add to instance metadata with gcloud compute instances add-metadata \
    # instance-name --metadata-from-file startup-script=auto-shutdown.sh
    # instance-name --metadata-from-file startup-script=idle-shutdown.sh
    # Modified from https://stackoverflow.com/questions/30556920/how-can-i-automatically-kill-idle-gce-instances-based-on-cpu-usage
    threshold=0.4

  10. @justinshenk justinshenk created this gist Jun 24, 2018.
    30 changes: 30 additions & 0 deletions auto-shutdown.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/bin/bash
    # Add to instance metadata with gcloud compute instances add-metadata \
    # instance-name --metadata-from-file startup-script=auto-shutdown.sh
    # Modified from https://stackoverflow.com/questions/30556920/how-can-i-automatically-kill-idle-gce-instances-based-on-cpu-usage
    threshold=0.4

    count=0
    while true
    do

    load=$(uptime | sed -e 's/.*load average: //g' | awk '{ print $1 }') # 1-minute average load
    res=$(echo $load'<'$threshold | bc -l)
    if (( $res ))
    then
    echo "Idling.."
    ((count+=1))
    fi
    echo "Idle minutes count = $count"

    if (( count>60 )) # number of minutes
    then
    echo Shutting down
    # wait a little bit more before actually pulling the plug
    sleep 300
    sudo poweroff
    fi

    sleep 60

    done