0

I need to stop server running at aws within 750 hours. Is there anyway i can automate it shutdown process within that time limit ?

JoKeR
  • 6,972
  • 9
  • 43
  • 65
George Ulahannan
  • 89
  • 1
  • 7
  • 17

1 Answers1

1

Here is a simple bash script to do this job (save this to a file with .sh extension):

#!/bin/bash

reboot=$((750*3600))
uptime=$(awk -F. '{print $1}' /proc/uptime)

if [ $uptime -ge $reboot ]; then
  shutdown -h
fi

Let me explain:

reboot=$((750*3600))

We get the representation of 750 hours in seconds

uptime=$(awk -F. '{print $1}' /proc/uptime)

This is the system uptime in seconds

After you saved the file give it executable permissions:

chmod +x name-of-your-file.sh

You can set a cron job to execute this script every hour:

sudo crontab -e

Then paste this:

0 * * * *  /path/to/your/script.sh
Sh1d0w
  • 1,348
  • Since you're using bash anyway, you could use shell arithmetic instead of [: if (( reboot > uptime )); then ... – muru Apr 06 '15 at 10:46