Search code examples
pythonwindowspowershellazurewindows-server-2012

VM Reboot: Run python flask server and webpack-dev server automatically on system start-up


Every time my VM reboots I need to do two things:

  1. Run my Flask server where API is hosted

  2. Run the webpack-dev server to serve front-end files

Is there a way in Windows Server where I can execute the above two command prompt commands i.e. python flaskserver.py and npm start to run automatically at system startup in cases where my VM reboots automatically?


Solution

  • To create a startup task in Windows Server 2012, we can leverage Task Scheduler in Administrative Tools. Here are the general steps:

    1, Create a file with the command script, i.e.

    cd C:\your_node_app_path npm start

    save it as a powershell file, i.e. startup.ps1.

    2, Click Create Basic Task in Task Scheduler.

    3, Set a name, click “Next”.

    4, Select when the computer starts in Trigger Tab.

    5, Select Start a program in Action Tab.

    6, Type cmd.exe in Program/script input, and type the command which will be ran in via cmd.exe, i.e. powershell –f <your_startup_ps1_file_path>. Click “next” and “finish”

    7, In the Task Scheduler Library we can find the task we created above, double click to edit its properties. enter image description here

    8, In General tab select “Run whether user is logged on or not” , In Conditions tab unmark “start the task only if the computer is on AC power” under the Power section. enter image description here

    And here is my test script shown in action tab. enter image description here