MagicMirror² v2.14.0 is available! For more information about this release, check out this topic.

Flushing pm2 logs

  • Moderator

    While keeping MM alive with the 'pm2' facility, all of the messages that MM generates will get logged through it. This allows you to be able to look at them with 'pm2 logs'. However, this also means that that log file is continuously growing.

    For those of you running the newsfeed module, every once in a while that will throw an error if the feed item has a problem, such as missing a title. That error will repeat each time the newsfeed cycles back to the beginning. Over time, this creates a large amount of logged errors and the physical log file will be rather large.

    I was recently rather surprised to find the storage on my rpi at 99% full when I know that it should be closer to 48% … After scanning the file system I found the pm2 MagicMirror specific log file at several gigabytes in size. Left unchecked, this would’ve eventually filled the file system to 100% and caused the rpi itself to start failing in various, unpredictable ways.

    So, ontop of my post about maintenance on your rpi, add this to the list: 'pm2 flush'. Run that as the user that’s running the MM task (usually that’s the 'pi' user) and it will automatically flush the log file for you.

    If you care to know where all this is stored on your rpi, it’s in the 'pi' user’s folder, under a (hidden) folder called '.pm2'. Looking at its contents:

    ├── conf.js
    ├── dump.pm2
    ├── logs
    │   ├── MagicMirror-error-0.log
    │   └── MagicMirror-out-0.log
    ├── module_conf.json
    ├── pids
    │   └──
    ├── pm2.log
    ├── pub.sock
    ├── rpc.sock
    └── touch
    2 directories, 11 files

    The files you should look at are the 'MagicMirror-error' and 'MagicMirror-out' ones. Running 'pm2 flush' will clear both of them.

    Edited to add: I forgot, another way to also see where the logs are, if by running 'pm2 show '. It should return a screen similar to this:

    pm2 show MagicMirror
     Describing process with id 0 - name MagicMirror 
    │ status            │ online                                     │
    │ name              │ MagicMirror                                │
    │ restarts          │ 1                                          │
    │ uptime            │ 14s                                        │
    │ script path       │ /home/pi/                    │
    │ script args       │ N/A                                        │
    │ error log path    │ /home/pi/.pm2/logs/MagicMirror-error-0.log │
    │ out log path      │ /home/pi/.pm2/logs/MagicMirror-out-0.log   │
    │ pid path          │ /home/pi/.pm2/pids/       │
    │ interpreter       │ bash                                       │
    │ interpreter args  │ N/A                                        │
    │ script id         │ 0                                          │
    │ exec cwd          │ /home/pi                                   │
    │ exec mode         │ fork_mode                                  │
    │ node.js version   │ N/A                                        │
    │ watch & reload    │ ✘                                          │
    │ unstable restarts │ 0                                          │
    │ created at        │ 2016-06-07T16:41:36.004Z                   │

  • Automatically log rotate all applications logs managed by PM2

  • Moderator

    Yes that’s another option, however personally I don’t use log rotates for a variety of reasons. In this case specifically, others have mentioned problems with it on the PM2 site.

  • I just want to add, that using pm2 flush in a script (for restart purposes) may not work as expected. The next operation will start immediately, before flush completes, and thus the new thread will already start writing to the file, blocking it from getting cleared.

    You need to wait for the entire thread to finish, like this:

    pm2 flush; pm2 start mm; echo "this doesn't clear the logs";
    pm2 flush &&  pm2 start mm; echo "now it works";

Log in to reply