How to Restart FastAPI Server with Bash Script
One simple trick to make your life easier
By reading this piece, you will learn to create a bash script that automates the restart process for your FastAPI server (production). As you have known, you can restart the server by using the reload argument during development.
uvicorn myapp:app --reload
When FastAPI (uvicorn ASGI) detects changes in the files, it will trigger a reload for you. However, you should not use the reload
argument for the production server. This is simply because it will hog your CPU resources in order to detect changes to the local files.
Most of the time, you are going to deploy it using nohup
and &
argument which allows the process to continue running in the background. As a result, there might arise situations in which you just want to restart FastAPI server:
- update to the configuration file
- bug fixes
- adding in new features
Manual Restart
My usual routine to restart FastAPI server is as follows:
- Check the process ID that runs FastAPI server
- Terminate or kill the processes
- Clean up nohup file
- Start FastAPI server