LoginSignup
0
3

More than 3 years have passed since last update.

EC2の起動時にPython(Flask)を起動する方法

Posted at

はじめに

EC2の起動時間が営業時間内に設定されている場合、毎朝コマンドを叩くのが面倒なので、
今回はpythonで作ったAPI(Flask)をshellを自動起動するようにしました。
参考サイトの方法を忘れないようにまとめたものです。

apiのshell化

自分は権限が足りなかったのでsudo

# sudo vim /usr/local/start_api.sh
----------------------------------
#!/bin/bash

nohup python3 /usr/local/api.py &

exit 0

自動起動の設定

ここでも権限が足りなかったので、sudoしました。

# sudo vim  /etc/init.d/api_start
-------------------------------
#!/bin/sh
# chkconfig: 345 99 10
# description: start_api shell
case "$1" in
  start)
    bash /usr/local/start_api.sh
       ;;
  stop)
     /usr/bin/kill python
       echo "stop!"
       ;;
  *) break ;;
esac

実行権限の付与

ここでもsudo

$ cd /etc/init.d
$ sudo chmod 775  api_start

自動起動への登録

$ chkconfig --add api_start

## 自動起動をonにする
$ chkconfig app_start on

## 設定されているかを確認する
$ chkconfig --list app_start

api_start       0:off   1:off   2:on    3:on    4:on    5:on    6:off

こうなれば終わり。

ちなみに、win10のコマンドプロンプロトの場合、ファイルの色が変わります。(白→緑)
image.png

image.png

最後に

インスタンスを再起動して、起動されているかを確認して、終わり。

参考サイト

https://hit.hateblo.jp/entry/aws/ec/initd
https://dev.classmethod.jp/articles/ec2shell/

0
3
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
3