MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Flushing pm2 logs

    Scheduled Pinned Locked Moved Core System
    6 Posts 4 Posters 14.6k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • KirAsh4K Offline
      KirAsh4 Moderator
      last edited by KirAsh4

      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:

      pi/.pm2
      ├── conf.js
      ├── dump.pm2
      ├── logs
      │   ├── MagicMirror-error-0.log
      │   └── MagicMirror-out-0.log
      ├── module_conf.json
      ├── pids
      │   └── MagicMirror-0.pid
      ├── pm2.log
      ├── pm2.pid
      ├── 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 <YOUR_MM_TASK>'. 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/MagicMirror.sh                    │
      │ 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/MagicMirror-0.pid       │
      │ 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                   │
      └───────────────────┴────────────────────────────────────────────┘
      

      A Life? Cool! Where can I download one of those from?

      1 Reply Last reply Reply Quote 0
      • C Offline
        CGabo
        last edited by

        Automatically log rotate all applications logs managed by PM2
        https://github.com/pm2-hive/pm2-logrotate

        1 Reply Last reply Reply Quote 0
        • KirAsh4K Offline
          KirAsh4 Moderator
          last edited by

          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.

          A Life? Cool! Where can I download one of those from?

          1 Reply Last reply Reply Quote 0
          • E Offline
            E3V3A
            last edited by

            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";
            

            "Everything I do (here) is for free – altruism is the way!"
            MMM-FlightsAbove, MMM-Tabulator, MMM-Assistant (co-maintainer)

            1 Reply Last reply Reply Quote 1
            • O Offline
              Ogden62
              last edited by

              This post is deleted!
              O 1 Reply Last reply Reply Quote 0
              • O Offline
                Ogden62 @Ogden62
                last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Enjoying MagicMirror? Please consider a donation!
                MagicMirror created by Michael Teeuw.
                Forum managed by Sam, technical setup by Karsten.
                This forum is using NodeBB as its core | Contributors
                Contact | Privacy Policy