0

I wrote a bash script to automate running my flask app:

#!/bin/bash                                                                                                           

python3 -m venv venv
. venv/bin/activate

export FLASK_APP=my_app
export FLASK_ENV=development
flask run

I'd like to change it to flask run & so that I can close the tab and still have it running, but I don't know how to stop the app if I've done it in this way. If I'm in a new tab and a new environment, what would I have to do to stop the app?

davidism
  • 121,510
  • 29
  • 395
  • 339
Frank Harris
  • 587
  • 2
  • 16

2 Answers2

0

To stop the process is necessary to keep the pid somewhere and then kill it. You could keep the pid like this:

./your-script.sh &
echo $! > save_pid.txt

Then if you want to kill the process:

kill $(cat save_pid.txt)
rm save_pid.txt
chepner
  • 497,756
  • 71
  • 530
  • 681
0

I am assuming you are using Linux / OS X etc.

One way to do it to "exec" the flask task in the bash script after recording the bash's PID for example:

#!/bin/bash                                                                                                           

python3 -m venv venv
. venv/bin/activate

export FLASK_APP=my_app
export FLASK_ENV=development
echo $$ > $HOME/.my_app.pid

exec flask run

Then you can check the contents of $HOME/.my_app.pid to know which pid to kill

The exec makes bash simply execute the flask run command replacing its own pid (the bash disappears)

The more interesting approach would be to use daemonize ... This is answered in details here:

How do I daemonize an arbitrary script in unix?

Hopefully this helps.

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58