<meta name="twitter:card" content="summary"> <meta name="twitter:image" content="https://ekbanaml.github.io/assets/images/ekbana/logo.png">
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.  It makes the deployment of an application/script simple and easy, also the development process by giving us an easy access of status and logs.
Normally, we were using it for serving the node js script. Normally when we serve an application with pm2 e.g
pm2 start app.js we will be able to see the application being running in daemon mode.
In the above screenshot, the file names are different, I hit two commands :
pm2 start server.js
Let us ignore the first process as of now, because that’s the python process which we’re going to discuss later. Now to see the logs of the ‘server.js’ application, we can simply do
pm2 logs server
pm2 logs to watch the log of all the processes currently running under pm2.
pm2 restart appnameto restart a single application or
pm2 restartto restart all the application at once.
pm2 reload appnameto reload a single application or
pm2 reloadto reload all the application at once.
pm2 delete appnameto delete a single application or
pm2 delete allto delete all application.
For python processes as well, you don’t have to do anything extra, you can simply do :
pm2 start pythonfilename.py
Replace pythonfilename with your desired python filename. Make sure you’re in the same directory where the file is available. But this way, pm2 seems to pick the default python of the system which is mostly python2.7, but we can direct pm2 to use python3 by explicitly providing an interpretar argument like this:
pm2 start detect_and_send_to_pi.py --interpreter /usr/bin/python3
This way, pm2 will pick the python3 to run the process.
Normally, there is the hassle of creating systemd service when it comes to creating a stable deployment that is untouched by the frequent system reboots. But pm2 makes it really easy to create services of the pm2 processes such that those processes are automatically up when the system reboots, or automatically reloaded when the application crashes.
This will list the processes, if you do not want some processes in the startup, you can delete them using
pm2 delete processname
pm2 startup # This will output some commands like this:
Now copy and paste the command in the terminal. Now pm2 initiates the auto-initialization for us in the server restart.
Optionally we can also choose which processes/application to choose for auto restart:
We can do
pm2 save to save all the currently initiated processes with
This will create a dump file and PM2 will automatically spawn this application at reboot.  Now even if you reboot your system, the pm2 processes will be up and running nicely.
The article is inspired by  the only new thing was the –interpreter command for python process.