Run a python Flask server as a windows background service using nssm

flask_nssm

Skill - Run a python Flask server as a windows background service using nssm

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


  • In this post we will learn how to run a flask server as a background service in windows using nssm

What is nssm

  • nssm runs commands or batch files (in our case, the flask application) as background services in windows
  • The services are also restarted in case of failure
  • The services can be easily managed using services.msc in windows
  • Command line output can be saved in log files as text
  • The generated logs can also be rotated based on time and log file size criteria
  • nssm also has a graphical editor to manage it’s services

Install nssm in windows

  • Download nssm zip file from https://nssm.cc/download and unzip into a folder in C drive
  • In the ‘Path’ system environment variable, add the path of nssm.exe, so that nssm.exe can be recognized in command line

Example Flask server

The following is an example flask server that we will run as a background service in windows

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=50100, debug=True)

Step 1 : Create a batch file to run the server

  • Create a batch file say run_server.bat that will run the python server in a command line. You can check by running the batch file. Keep the batch file in the same folder as the ‘server.py’ file.
call python server.py

Step 2 : Use nssm to run the batch file as a background service

  • Open a command prompt as an administrator. Change the directory of the command prompt to the directory where the ‘run_server.bat’ is present using ‘cd’ command.
  • Run the following commands to run the batch file as a background service
call nssm.exe install my_flask_app "%cd%\run_server.bat"
call nssm.exe set my_flask_app AppStdout "%cd%\logs\my_flask_app_logs.log"
call nssm.exe set my_flask_app AppStderr "%cd%\logs\my_flask_app_logs.log"
call nssm set my_flask_app AppRotateFiles 1
call nssm set my_flask_app AppRotateOnline 1
call nssm set my_flask_app AppRotateSeconds 86400
call nssm set my_flask_app AppRotateBytes 1048576
call sc start my_flask_app

The commands are explained as shown below

  • nssm.exe install my_flask_app "%cd%\run_server.bat" will register a background service named “my_flask_app” and that runs the command "%cd%\run_server.bat". Here %cd% means the ‘current directory’
  • The below commands will set the file paths to log the output and error streams of command line.
nssm.exe set my_flask_app AppStdout "%cd%\logs\mis_dashboard.log"
nssm.exe set my_flask_app AppStderr "%cd%\logs\mis_dashboard.log"

Ensure there is a folder named ‘logs’ in the folder containing ‘server.py’ file.

  • nssm set my_flask_app AppRotateFiles 1 will enable log rotation
  • nssm set my_flask_app AppRotateOnline 1 will rotate log files even if application is running
  • nssm set my_flask_app AppRotateSeconds 86400 will rotate log files after 86400 seconds (1 day)
  • nssm set my_flask_app AppRotateBytes 1048576 will rotate log files after log files reaches 1048576 bytes (1 MB) size
  • sc start my_flask_app will start the background service

nssm_install_demo

Edit the service using nssm GUI

nssm edit my_flask_app

nssm_edit_service_demo

Manage windows background services

  • Open Services app or run “services.msc” to open the services app in windows
  • The service can be started and stopped in this application

nssm_services_msc_demo

‘sc’ command to manage services in command line

  • Delete a service - sc delete my_flask_app
  • Start a service - sc start my_flask_app
  • Pause a service - sc pause my_flask_app
  • Stop a service - sc stop my_flask_app

View status of a service in command line

sc query my_flask_app

sc_query_demo

Video

The video for this post can be seen here

References

Comments