0
0

More than 1 year has passed since last update.

ec2を再起動するたびに特定のコマンド実行

Posted at

ec2起動時にはアプリのバイナリファイルを実行するコマンド

nohup ./main &

をターミナル上で打つ。
ただサーバー代をケチっているため、夜はインスタンスを停止させ、朝にインスタンスを再起動?している。
そのため毎朝ターミナルにssh接続をして上記コマンドを打たないといけない。
今回はそれがとても面倒に感じたためインスタンス再起動時に自動でbashスクリプトを読み込む設定をしてみた。

参考にさせていただいたURL
https://qiita.com/Ueken3pei/items/003600c9b1c8435af548

上記のページで言うとおりに
ec2インスタンスのユーザーデータの箇所にテキストベースで
MIME-MULTIPART形式でかけばよい。上記のコマンドの場合だと

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
nohup ./main &
--//--

とユーザーデータを編集して保存すると、再起動しても毎回このコマンドが打ち込まれるため、とても便利になる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0