pm2 - madBeavis/PimpMyAtv GitHub Wiki

WIP - Work in progress

Foreward

Running MAD and its associated apps 24/7/365 is a challenge, so why not use some software to make your life easier. I run it on my local MAD scanning laptop, send data to my VPS for maps and have a few instances of pokealarm running on seperate VPS servers I manage. Things just run, get restarted as needed and startup on boot. None of this "my map doens't work" because I didn't remember to get RocketMAD going in a terminal, but I tell them "I will look when I get home and btw see you next Tuesday" while I grumble to myself they should just do their own scanner if they weren't such a whinging bish.

You can run MAD as a service, but that seems overly complicated compared to alternatives. I have to go through major hoops just to find and remember, let alone restart my daemon that runs my SSH tunnel. If something goes wrong, I usually just reboot as it is quicker instead of finding/starting/stopping/troubleshooting the daemon.

I won't even touch Docker here. I run it for Flo's TileServer on my VPS for Discord alerts because that is how the instructions are written. Docker confounds me on many levels, I won't use unless I absolutely have to do it. In case I suddenly become wrong, I will google when I am bored to see if i can find a reason for using Docker, but I can't beyond programs like the tileserver that were setup for it.

Your life will be easier with pm2, a simple and effective solution to what ails daemons and management of MAD and related programs. As not in the case of daemons, the commands for pm2 processes are easy, i.e. "pm2 ls".

Advantages:

  1. Easy to use
  2. Easy to install
  3. Easy to add programs to it
  4. Programs can be setup to restart with the operating system
  5. Updates to programs follow a familiar and simple workflow
  6. When processes crash, they get restarted
  7. Far more reliable than using ssh to run programs such as MAD. Using ssh from home to VPS to operate MAD dies when the connection dies or the terminal crashes. Don't be that guy/girl/whatever.

Disadvantages:

  1. On occasion I type pms instead of pm2. Edits - I have been directed that the first rule of a club is to not talk about said club. Probably not worth figuring out if it is psychological projection, trauma or something freudian...

Installation of pm2

Installation of pm2 is easy, done with the following directions. I will assume that you have an appropriate version of npm already, since it is required for programs associated with MAD. The command to install is simply pm2 install pm2-logrotate.

There is a lot you can do with pm2. Once you have got things installed, one can refer to the pm2 page and/or the following quick start for anything beyond the basics described here.

Controlling logs

pm2 has a few modules that can be added to it that could prove useful. I use logrotate to control log files. I followed the instructions to setup on the prior page.

I wondered why my VPS disk space used was growing, so I googled and found that pm2 will keep a highly excessive amount of logs by default. It doesn't make much sense to keep many logs in pm2 as I would only be interested in the ones in the respective program. I used the command pm2 set pm2-logrotate:max_size 1M to limit their size and pm2 set pm2-logrotate:retain 2 to limit their count to 2.

pm2 watchdog

There is another module for pm2 called watchdog. I have not used, just putting it out there so that others may utilize if they find a use case for it. Watchdog can be found on its github. According to the readme, it Module is continuously checking the health of PM2 NodeJS processes by calling the specified HTTP(s) endpoint. If the HTTP server is unavailable or response is not 2xx, the PM2 process will be restarted.

Persisting settings

You must do a few things to get pm2 to persist processes across reboots. Once you get things the way you like, go ahead and do pm2 save to save your process list. To get things working at startup, use the command pm2 startup to generate a startup script and follow the directions. Use some googlefoo if you run into further issues. If you update processes, don't forget to save them as the warnings will tell you.

Updating MAD

You can setup pm2 to monitor directories and restart the process when files change. I don't do it, doesn't make sense when you git pull and should update requirements anyways. Seems more reasonable to pm2 stop mad, do your usual update process and then restart with pm2 start mad.

A few useful commands

The following are tips/tricks/commands that I use often:

  1. When referring to processes, you can use their name or index
  2. pm2 ls
    1. lists processes
  3. pm2 logs processname
    1. view logs coming from processname
  4. pm2 describe processname
    1. gives you relevant information regarding the process
  5. pm2 delete processname
    1. delete the process
  6. pm2 monit
    1. monitor pm2 processes

Various programs associated with MAD

Before I add programs to pm2, I always run them as normal to verify they are operational and functioning as intended from the command line.

The following are commands I used to add various programs I run under the management umbrella of pm2. All programs are started from their appropriate directories. For what I have done on mine, there is no need to do complicated paths and cd commands to setup the pm2 task, i.e. pm2 start --name MADMIN "cd /path/to/mad/ && /path/to/venv/bin/python3 /path/to/mad/start.py" -u mad --hp /home/mad/. Simply cd MAD and to the below command for adding mad from there. Don't over complicate things, always KISS.

Don't know what a virtual environment is? Exercise some googlefu and thou shall find plenty of answers. Some examples are using venv and some not just to spice things up to show examples of both, adjust for your setup.

The examples are as follows:

  1. MAD without venv pm2 start start.py --name="mad" --interpreter="python3"
  2. MAD using venv pm2 start start.py --name="mad-venv" --interpreter="/path/to/your_mad_venv/bin/python3"
  3. hypothetical instance of MAD that gets restarted at midnite pm2 start start.py --name="mad-special" --interpreter="/path/to/your_mad_venv/bin/python3" --cron="0 0 * * *"
  4. Pokealarm
    • Python2.7 pm2 start start_pokealarm.py --name="pa" --interpreter="python"
    • Python3 pm2 start start_pokealarm.py --name="pa" --interpreter="/path/to/your_pa_venv/bin/python3"
  5. Pokealarm for PVP
    • Python2.7 pm2 start start_pokealarm.py --name="pa-pvp" --interpreter="python"
    • Python3 pm2 start start_pokealarm.py --name="pa-pvp" --interpreter="/path/to/your_pa-pvp_venv/bin/python3"
  6. RebootMadDevice pm2 start CheckMadDevices.py --name="rmd" --interpreter="python3"
  7. A leveling instance of MAD pm2 start start.py --name="level" --interpreter="python3"
  8. RocketMAD pm2 start runserver.py --name="rm" --interpreter="python3"
  9. Poracle pm2 start src/app.js --name poracle
  10. MADevice pm2 start main.py --name="MADevice" --interpreter="/path/to/your_pa-pvp_venv/bin/python3"
  11. namefetcher and stopwatcher using pm2.
    • be sure to use the --no-autorestart flag so you don't hammer the backend. this flag keeps pm2 from restarting the process when it stops. it will be restarted by the --cron-restart flag
    • i set my namefetcher to run before stopwatcher every hour to get alerted to retrieve new names and then get alerts
    • be sure to set the no-autorestart flag, as they will continually run otherwise
    • setup stopwatcher to run on pm2
      • pm2 start stop_watcher.py --interpreter="python3" --cron-restart "20 */1 * * *" --name "stopwatcher" --no-autorestart
    • do the same for namefetcher
      • pm2 start name_fetcher.js --cron-restart "10 */1 * * *" --interpreter "node" --name "name_fetcher" --no-autorestart
  12. Command line parameters There are many, google for them or check from some of the links provided above. For example, pm2 start start.py -v does not work as intended. To do command line parameters, you must specify them as say pm2 start start.py -- -cf SomeCrazy2ndConfigRunningMadFromSameDirectory.ini to start a second instance of mad from the same directory by specifying a different config file.

Cron Commands

I didn't don't have enough material to make a specific cron page, but wanted to throw a few nice commands that can help your setup.

  1. Restart my main mad instance with cron every day (be sure to point to your node and pm2 on your distro)
    1. 1 0 * * * /usr/bin/node /usr/local/bin/pm2 restart mad
  2. Update the wizard to latest versions, handy so you don't even have to mess with wizard once new versions of software when they become available
    1. 0 19 * * * curl -X POST --user 'MadminUsername:MadminPassword' -H 'Content-Type: application/json-rpc' -d '{"call": "search_download"}' http://192.168.8.200/api/mad_apk >> /home/user/mad>
    2. source on discord