How to start Python (Flask) when EC2 starts

Introduction

If the EC2 startup time is set during business hours, it is troublesome to hit the command every morning, so This time, the API (Flask) made with python is used to automatically start the shell. It is a summary so as not to forget the method of the reference site.

api shell

I didn't have enough privileges so sudo

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

nohup python3 /usr/local/api.py &

exit 0

Auto start settings

Again, I didn't have enough privileges, so I sudoed it.

# 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

Grant execution authority

Again sudo

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

Registration for automatic startup

$ chkconfig --add api_start

##Turn on auto-start
$ chkconfig app_start on

##Check if it is set
$ chkconfig --list app_start

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

This is the end.

By the way, in the case of the command prompt of win10, the color of the file changes. (White → green) image.png

image.png

Finally

Reboot the instance, make sure it's started, and you're done.

Reference site

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

Recommended Posts

How to start Python (Flask) when EC2 starts
How to upload with Heroku, Flask, Python, Git (4)
[2020 version] How to install Python3 on AWS EC2
How to install Python
How to install python
How to upload with Heroku, Flask, Python, Git (Part 3)
How to upload with Heroku, Flask, Python, Git (Part 1)
How to upload with Heroku, Flask, Python, Git (Part 2)
How to exit when using Python in Terminal (Mac)
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Start to Selenium using python
3 Reasons Beginners to Start Python
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to start the program
How to use Python bytes
How to install python using anaconda
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to collect images in Python
Preparing to run Flask on EC2
Flask reuse How to write html
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to get the Python version
[EC2] How to run selenium webdriver
How to add sudo when debugging
How to get started with Python
[Python] How to import the library
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
[Python] How to swap array values
How to wrap C in Python
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to speed up Python calculations
How to calculate date with python
How to access wikipedia from python
How to use python zip function