1

I have created a shell script for both Bash and PowerShell that creates a python venv, enters it, installs requirements and runs the django migrations and server, like so:

cd my-proj
python -m virtualenv venv
source venv/bin/activate #### .\venv\Scripts\activate FOR PS
pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
deactivate
cd ..

In Bash, when I stop the server with CTRL-C, the script will continue its execution, so it will correctly exit the venv and return to the previous folder.

But not in PowerShell. In PS it will stop the whole script. I would like to safely stop the runserver command by catching the CTRL-C. Is it possible?

  • 1
    In short: PowerShell allows you to perform _cleanup_ operations when a script is stopped via Ctrl-C, via the `finally` block of a `try` / `catch` / `finally` statement. That is, your script will _invariably terminate_, but you can perform - limited - actions just before termination happens. See the linked duplicate for details. – mklement0 Jul 07 '23 at 14:49

0 Answers0