paint-brush
How To Continuously Run a NodeJS or Python App While Keeping Them Updatedby@codejedi
1,732 reads
1,732 reads

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

by Code_JediAugust 25th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Using 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.
featured image - How To Continuously Run a NodeJS or Python App While Keeping Them Updated
Code_Jedi HackerNoon profile picture

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 &