/var/www/yatta47.log

/var/www/yatta47.log

やったのログ置場です。スクラップみたいな短編が多いかと。

EC2を既定の時間になったらシャットダウンする

f:id:yatta47:20210105004543p:plain

コスト削減のためにEC2を既定の時間になったら落としたい。

AWS上なので、lambda + Cloudwatch Eventとかでやる方法が多いんだろうけど、自分の場合たまたま自分の開発用のサーバとして使っているのがEC2という状況なのでそこまで仰々しいことはやりたくない。

もっとシンプルな方法ないだろうかと考えていて、気が付きました。

「cronで指定して自分で落とせばいいじゃないか」

ってことでcrontabに以下の設定をすることで解決しました。(最後の行を追加)

$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
00 13   * * *   root /sbin/shutdown -h now

時刻はGMTでサーバを動かしているので、日本時間だと+9時間した時間を指定。 これで23時になったらサーバが勝手に落ちます。あっ。ちなみにサーバOSはUbuntu18です。

解決!