DeadPool
DeadPool

Reputation: 255

How to restart my python app automatically in AWS CodeDeploy

I made few changes in my botMain.py file. CodeDeploy is successful but the changes are not effective in the app. So, I edited my RunMyBot.sh file but still there's no change.

appspec.yml

version: 0.0
os: linux
files:
  - source: /
    destination: /home/mybot
hooks:
  AfterInstall:
    - location: scripts/install_dependencies.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/RunMyBot.sh
      timeout: 300
      runas: root

RunMyBot.sh (new)

#!bin/bash

sudo /usr/bin/pm2 restart myBot
nohup python3 botMain.py & /dev/null 2> /dev/null < /dev/null &

RunMyBot.sh (old)

serverfile="/lib/systemd/system/mypythonservice.service"
echo "[Unit]" > $serverfile
echo "Description=My Python Service" > $serverfile
echo "After=multi-user.target" >> $serverfile

echo "[Service]" >> $serverfile
echo "Type=idle" >> $serverfile
echo "ExecStart=/usr/bin/python /home/mybot/botMain.py" >> $serverfile
echo "Restart=on-failure" >> $serverfile

echo "[Install]" >> $serverfile
echo "WantedBy=multi-user.target" >> $serverfile
cat $serverfile

sudo chmod 644 /lib/systemd/system/mypythonservice.service

sudo systemctl daemon-reload

sudo systemctl enable mypythonservice.service

The same server file script is in my instance user data also so I removed it from RunMyBot.sh

Upvotes: 0

Views: 1055

Answers (2)

Cloud Karamchari
Cloud Karamchari

Reputation: 116

you can use BeforeInstall hook to stop the running services and then deploy your new code in VM. After then, you can start your services.

AppSpec example : https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html

Upvotes: 0

Marcin
Marcin

Reputation: 238687

Before you deploy new version of your app, you have to stop your existing nohup. You could do this by adding ApplicationStop section to your appspec.yml.

I think the old setup was better, albeit seemingly more difficult to setup at first. With the old setup you would just restart your daemon.

Upvotes: 2

Related Questions