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?
CodePudding user response:
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.
CodePudding user response:
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
