I need to stop server running at aws within 750 hours. Is there anyway i can automate it shutdown process within that time limit ?
Asked
Active
Viewed 101 times
0
-
Do you want to stop only the apache service or to shutdown the system completely? – Sh1d0w Apr 06 '15 at 10:21
-
I need to stop the system completely. – George Ulahannan Apr 06 '15 at 10:22
1 Answers
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