How To Continuously Run a NodeJS or Python App While Keeping Them Updated

Written by codejedi | Published 2023/08/25
Tech Story Tags: devops | nodejs | python | python-programming | node | bash | nodemon | nodejs-tutorial

TLDRUsing a seemingly random combination of bash commands and parameters, in this post I explain how you can continuously run Python and NodeJS apps while also keeping them updated.via the TL;DR App

While I was deploying my NodeJS and Python apps to a Google Cloud VM instance, I spent some time trying to figure out how to run my apps so that they don’t terminate and that they update upon any changes.

This is the command that I found to work for NodeJS:

nohup nodemon index.js </dev/null &

If you don’t have nodemon installed, you can install it using:

npm i -g nodemon

Let me explain in more detail. The “nodemon” is the part of the command that actually runs the script while also automatically restarting it upon any updates. The “nohup” part makes the script keep running even if you exit your VM or kill your terminal.

The “&” sign at the end makes the script run in the background, and “</dev/null” is the part that allows nodemon to run with nohup by allowing it to run without waiting for input.

Something that a lot of people may not know is that you can use nodemon with Python just like with NodeJS! All you need to do is add the --exec python3 argument.

nohup nodemon --exec python3 main.py </dev/null &

Using this command, you can run your Python script continuously while keeping it updated.

In case you’re worried that you won’t be able to stop a continuous processes later, don’t worry, all you have to do is run ps aux and after getting a table similar to the below…

…get the PID of your nodemon process (223005 in this case) and run sudo kill 223005.

By running ps aux you can also check if your processes are still running.

Now if you want to redirect your scripts’ output to an output file instead of the default nohup.out file to avoid mixing outputs from multiple scripts, you can specify your output file between your script name and the “</dev/null” parameter using a “>” like this:

nohup nodemon index.js > output.txt </dev/null &

You can do the same with Python:

nohup nodemon --exec python3 main.py > output.txt </dev/null &


Written by codejedi | Python, Machine Learning, Web-Scraping, Web-Automation and more...
Published by HackerNoon on 2023/08/25